0

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 ////

 }
Rmurp006
  • 37
  • 5

1 Answers1

0

You need usort.

have a look at this post https://stackoverflow.com/a/2699159/2209160

in your case:

usort($shop, function($a, $b) {
   return $a[0] > $b[0];
});

if your php version is >= 5.3

Community
  • 1
  • 1
Marius.C
  • 700
  • 6
  • 14
  • usort worked perfectly. Everything is in order now. However it still selects every field from the array to show on each line. How would I use the list function to display just 2 of the fields in the array? '//// SORT BY NUMERICAL DISTANCE (SORT BY '0'. TO SORT BY CITY = 'BUS_NAME') //// function sortByOrder($a, $b) { return $a['0'] - $b['0']; } usort($store, 'sortByOrder'); //// END SORT BY NUMERICAL DISTANCE //// echo "

    "; foreach ($store as $v1) { foreach ($v1 as $v2) { echo "$v2\n"; } echo "
    "; }'
    – Rmurp006 Jan 30 '14 at 17:55
  • you want to limit the $store to 2 results(most relevant maybe) use array_slice ? otherwise I don't understand what you try to do. post the result you want to achieve maybe. – Marius.C Jan 30 '14 at 18:05
  • nevermind I figured it out. while loop is what i needed. $x=0; while($x<$num_of_bus) { echo "".$store[$x][bus_name]." in ".$store[$x][city]." (".$store[$x][0]." miles away)
    "; $x++; }
    – Rmurp006 Jan 31 '14 at 06:03