I created a database and found a code online that lets you search a zip or city, and it will look into the database to find the businesses within so many miles of the searched location. My problem is, after the query, I want the businesses shown to be ordered by distance from the entered location. However, the distance is different with every search and not something you can pull from the database. So I am thinking this may be a good place for an array?
Pull the businesses from the database out of order, put them into an array in order (shortest distance first), then display the array in the correct order for people to see. My question is, is there an easier way? I'll post some code below of what I have so far. I am pretty stumped right now though. Thanks for any help. Also, I cant seem to display the array in order? And obviously the very bottom array is based off only 3 businesses found..when there could be several at a time. I just don't know how to adjust that automatically? the $distance1 code below works great. $distance1 does show correct distances for all businesses rendered. Thanks!
//// BASED ON ARRAY FROM ABOVE, PULL BUSINESSES WITHIN THE MILES SELECTED ////
$query1 = "SELECT * FROM `list` WHERE `zip` IN (".implode(',',$locations).")";
$result1 = mysql_query($query1) or die ("<br /><br />There's an error in the MySQL-query: ".mysql_error());
while ($row1 = mysql_fetch_assoc($result1)) {
$bus_name = $row1["bus_name"];
$bus_city = $row1["city"];
$bus_state = $row1["state"];
$bus_zip = $row1["zip"];
//// DISTANCE BETWEEN SEARCH START AND END ////
$distance1 = round($searched2_zip->getDistanceTo($bus_zip), 2);
//// ADD DISTANCE TO SQL QUERY ////
array_push($row1, $distance1);
//// MAKE ALL ROWS A GIANT ARRAY ////
$store[] = $row1;
}
//// MULTIDIMENSIONAL ARRAY --> SHOW 2 VALUES (DISTANCE AND BUS_NAME) ////
$shop = array( array($store[0][0],$store[0][bus_name]),
array($store[1][0],$store[1][bus_name]),
array($store[2][0],$store[2][bus_name])
);
sort($shop); //// SORT BY NUMERICAL DISTANCE ////
}
"; foreach ($store as $v1) { foreach ($v1 as $v2) { echo "$v2\n"; } echo "
"; }' – Rmurp006 Jan 30 '14 at 17:55
"; $x++; } – Rmurp006 Jan 31 '14 at 06:03