Forum Moderators: coopster
$current_lon = 47;
$current_lat = 12; // Static for example only
$accuracy = 15; // Degree of accuracy desired
$sql = 'SELECT location_id, longitude, latitude FROM locations';
$result = mysql_query($sql);
$rows = mysql_num_rows($result);
$eligible = array();
for($i = 0; $i < $rows; $i++) {
$lon = mysql_result($result, $i, "longitude");
$lat = mysql_result($result, $i, "latitude");
$loca = mysql_result($result, $i, "location_id");
$lon_diff = abs($current_lon - $lon);
$lat_diff = abs($current_lat - $lat);
$distance = sqrt(pow($lon_diff, 2) + pow($lan_diff, 2));
if($distance <= $accuracy) {
$eligible[$loca] = $distance;
}
}
arsort($eligible); /* may need to use asort() instead of arsort() - not sure */
foreach($eligible as $loc => $dist) {
$sql = 'SELECT location_name FROM locations WHERE location_id = "' . $loc . '"';
$result = mysql_query($sql);
// Build your table here
}
$sql = 'SELECT location_id, longitude, latitude FROM locations WHERE longitude <= ' . ($current_lon + $accuracy) . ' AND longitude >= ' . ($current_lon - $accuracy) . ' AND latitude <= ' . ($current_lat + $accuracy) . ' AND latitude >= ' . ($current_lat - $accuracy);