[nycphp-talk] Multiple query from out put
thegeek
thegeek at thecolorgeek.com
Tue Sep 20 10:23:50 EDT 2005
Thanks for the help already I am still trying to wrap my monkey brain aroung
the concept of arrays but have found some good defenitions through google.
I am using this code I have found at phpclasses by: Micah Carrick have asked
for snippets hope this is not to much
function get_zips_in_range($zip, $range) {
// returns an array of the zip codes within $range of $zip. Returns
// an array with keys as zip codes and values as the distance from
// the zipcode defined in $zip.
$details = $this->get_zip_point($zip); // base zip details
if (empty($details)) return;
// This portion of the routine calculates the minimum and maximum lat
and
// long within a given range. This portion of the code was written
// by Jeff Bearer (http://www.jeffbearer.com). This significanly
decreases
// the time it takes to execute a query. My demo took 3.2 seconds in
// v1.0.0 and now executes in 0.4 seconds! Greate job Jeff!
// Find Max - Min Lat / Long for Radius and zero point and query
// only zips in that range.
$lat_range = $range/69.172;
$lon_range = abs($range/(cos($details[0]) * 69.172));
$min_lat = number_format($details[0] - $lat_range, "4", ".", "");
$max_lat = number_format($details[0] + $lat_range, "4", ".", "");
$min_lon = number_format($details[1] - $lon_range, "4", ".", "");
$max_lon = number_format($details[1] + $lon_range, "4", ".", "");
$return = array(); // declared here for scope
$sql = "SELECT zip_code, lattitude, longitude FROM zip_code
WHERE zip_code <> $zip AND lattitude BETWEEN '$min_lat' AND
'$max_lat' AND longitude BETWEEN '$min_lon' AND '$max_lon'";
$r = mysql_query($sql);
if (!$r) { // sql error
$this->last_error = mysql_error();
return;
} else {
while ($row = mysql_fetch_array($r)) {
// loop through all 40 some thousand zip codes and determine
whether
// or not it's within the specified range.
$dist =
$this->calculate_mileage($details[0],$row[1],$details[1],$row[2]);
if ($this->units == 'k') $dist = $dist * 1.609344;
if ($dist <= $range) {
$return[str_pad($row[0], 5, "0", STR_PAD_LEFT)] = round($dist,
$this->decimals);
}
}
mysql_free_result($r);
}
Currently I am able to post to the script with a zip and a range and echo
the results(a range of zips). Thanks for the pointers already.
Enjoy the Day
More information about the talk
mailing list