circuit Posted September 19, 2008 Share Posted September 19, 2008 I am trying to apply some changes to my site but my normal tech is on vacation if someone could please help me It would be highly appreciated. The code below calculates the distance between 2 zip codes and displays the distance in miles between them. It receives the latitued and longitude from a database on my server. The second set of code finds all the businesses in my database within a selected radius of the entered zip code. It currently displays all the locations at random. I am wondering whether it is possible to display all locations by the distance they are from the entered zip code. <?php function calculate_distance($lat1, $lon1, $lat2, $lon2) { $lat_diff = abs($lat1 - $lat2); $lon_diff = abs($lon1 - $lon2); $dist = ($lat_diff * $lat_diff) + ($lon_diff * $lon_diff); $dist = sqrt($dist) * 75; return number_format($dist, 2); } include_once("db_include.php"); $prop = 75; $zip = $_GET['zip']; $radius = $_GET['radius']; $get_center = mysql_query("SELECT * FROM zipcode WHERE zipcode LIKE '$zip'") or die(mysql_error()); if (mysql_num_rows($get_center) >= 1) { $cent = mysql_fetch_object($get_center); $latitude_min = $cent->latitude - ($radius / $prop) $latitude_max = $cent->latitude + ($radius / $prop) $longitude_min = $cent->longitude - ($radius / $prop) $longitude_max = $cent->longitude + ($radius / $prop) if ($cent->long_sign == 0) $get_codes = mysql_query("SELECT * FROM zipcode WHERE latitude < '$latitude_max' AND latitude > '$latitude_min' AND longitude < '$longitude_max' AND longitude > '$longitude_min'") else { $get_codes = mysql_query("SELECT * FROM zipcode WHERE latitude < '$latitude_max' AND latitude > '$latitude_min' AND longitude < '$longitude_max' AND longitude > '$longitude_min'") } $no_rec = true if (mysql_num_rows($get_codes) > 0) { while ($row = mysql_fetch_array($get_codes)) { $get_the = mysql_query("SELECT * FROM station WHERE zip = '$row[zipcode]'") if (mysql_num_rows($get_the) > 0) { $no_rec = false while ($recc = mysql_fetch_array($get_the)) { $get_s = mysql_query("SELECT * FROM zipcode WHERE zipcode LIKE '$recc[zip]'") $dist_val = mysql_fetch_object($get_s) $acc_dist = calculate_distance($cent->latitude, $cent->longitude, $dist_val->latitude, $dist_val->longitude) echo "<b>" . $recc['name'] . "</b><br />" echo $recc['address'] . "<br />" echo $recc['city'] . ", " . $recc['state'] . " " . $recc['zip'] . "<br />" echo $recc['phone'] . "<br />" echo "About " . $acc_dist . " miles" . "<br /><br />" } } } } $get_exact = mysql_query("SELECT * FROM station WHERE zip = '$zip'") if (mysql_num_rows($get_exact) == 0) { if ($no_rec) echo "No matches found" } else { while ($sta = mysql_fetch_array($get_exact)) { echo "<b>" . $sta['name'] . "</b><br />" echo $sta['address'] . "<br />" echo $sta['city'] . ", " . $sta['state'] . " " . $sta['zip'] . "<br />" echo $sta['phone'] . "<br />" echo "About " . $acc_dist . " miles " . "<br /><br />" ; } } } ?> Any input is highly appreciated. Link to comment https://forums.phpfreaks.com/topic/125014-listing-data-results-by-distance/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.