Forum Moderators: coopster
[webmasterworld.com...]
[example]
$nearest_distance = array();
$findLocations = mysql_query("SELECT latitude, longitude FROM locations");
while ($loop_findLocations = mysql_fetch_array($findLocations)) {
$latitude = $loop_findLocations["latitude"];
$longitude = $loop_findLocations["longitude"];
$yarddistance = distance($latitude, $longitude, $cutomerlat, $cutomerlong, "m");
$nearest_distance[] = $yarddistance;
}
sort($nearest_distance);
print_r($nearest_distance); // print array
echo '<br> Best Yard:'. $nearest_distance[0];
[/example]
The problem I'm having is that I want to be able to obtain the ID of the row that was used for the values in the calculation. Is this possible?
Let me clarify. The query loops through all of the rows in the table "location" inputting the values longitude and latitude into the calculation of distance function.
distance($latitude, $longitude, $cutomerlat, $cutomerlong, "m");
the array returned is all of the calculation values. They are sorted and the smallest value is returned.
So lets say the smallest value returned was calculated using the longitude and latitude of a row with an ID of 9, I want to be able to retrieve the ID as well.
Does this help? Or would it be useful for me to post the entire code and function together?
$nearest_distance = array();
$findLocations = mysql_query("SELECT id,latitude, longitude FROM locations");
while ($loop_findLocations = mysql_fetch_array($findLocations)) {
$latitude = $loop_findLocations["latitude"];
$longitude = $loop_findLocations["longitude"];
$yarddistance = distance($latitude, $longitude, $cutomerlat, $cutomerlong, "m");
$nearest_distance[] = $loop_findLocations['id'] . ',' . $yarddistance;
}
sort($nearest_distance);
print_r($nearest_distance); // print array
$data = explode(",", $nearest_distance[0]);
echo '<br> Best Yard:'. $data[1];
echo '<br> Row ID:'. $data[0];
Think that should work ok.
dc
1,334.2; 2,234.22; 3,9.02;
the sort should return 3 option, but will return 1.
Therefore I would choose another way of specifying the nearest location (Do you need just nearest location, or a few of them, eg. 3 nearest?)
$nearest_distance = 9999999999999999;//some big number. maybe 1e30 will work as well
$findLocations = mysql_query("SELECT id, latitude, longitude FROM locations");
while ($loop_findLocations = mysql_fetch_array($findLocations)) {
$latitude = $loop_findLocations["latitude"];
$longitude = $loop_findLocations["longitude"];
$yarddistance = distance($latitude, $longitude, $cutomerlat, $cutomerlong, "m");
if($nearest_distance > $yarddistance){
$nearest_id = $loop_findLocations["id"];
$nearest_distance = $yarddistance;
}
Best regards
Michal Cibor
This should work ok.
$nearest_distance = array();
$findLocations = mysql_query("SELECT id,latitude, longitude FROM locations");
while ($loop_findLocations = mysql_fetch_array($findLocations)) {
$latitude = $loop_findLocations["latitude"];
$longitude = $loop_findLocations["longitude"];
$yarddistance = distance($latitude, $longitude, $cutomerlat, $cutomerlong, "m");
$nearest_distance[$loop_findLocations['id']] = $yarddistance;
}
asort($nearest_distance);
print_r($nearest_distance); // print array
echo '<br> Best Yard: '. $nearest_distance[key($nearest_distance)];
echo '<br> Row ID: '. key($nearest_distance);
dc