Forum Moderators: coopster

Message Too Old, No Replies

Using an array for a calculation

calculations using arrays

         

aquim

5:12 pm on Jul 30, 2005 (gmt 0)

10+ Year Member



I am currently using a an array to perform a calculation similar to

[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?

dreamcatcher

7:46 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi aquim,

I`m just a little bit confused, so help me out here. You say you want to get the ID number of the row yet your query has no WHERE clause, so won`t this loop through and pull X amount of rows and not just one?

dc

aquim

8:44 pm on Jul 30, 2005 (gmt 0)

10+ Year Member



Thank you for the reply.

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?

dreamcatcher

9:06 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can add the Row ID to the array and use explode if you want to:


$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

dreamcatcher

9:12 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[edit]Updated row id variable. $id wasn`t correct.

dc

mcibor

9:48 pm on Jul 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This won't work as expected dreamcatcher... You created an array of strings:

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

aquim

2:33 am on Jul 31, 2005 (gmt 0)

10+ Year Member



I'm simply looking for the nearest location. Thank you.

dreamcatcher

8:26 am on Jul 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for clearing that up mcibor.

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

aquim

5:15 pm on Jul 31, 2005 (gmt 0)

10+ Year Member



Exactly what I needed. You guys have been a great help thanks!

dreamcatcher

7:58 pm on Jul 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



aquim, you are very welcome.

dc