0

I am working through a SQL query, and I would like to sort it by a function that I've used to get the distance between two points. The distance is not a part of the SQL query, but I want the results to show in ASC order based on what the function returns. Here's my code:

/**Function***/

function distance($lat1, $lon1, $lat2, $lon2, $unit) {

                            $theta = $lon1 - $lon2;
                            $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
                            $dist = acos($dist);
                            $dist = rad2deg($dist);
                            $miles = $dist * 60 * 1.1515;
                            $unit = strtoupper($unit);

                                if ($unit == "K") {
                                return ($miles * 1.609344);
                                 } else if ($unit == "N") {
                                return ($miles * 0.8684);
                                 } else {
                                   return $miles;
                                   }
                                }       


  /** Add sql query here**/
            $dbc = mysqli_connect(HOSTNAME,DB_USERNAME,DB_PASSWORD,DB_NAME);

            //Build query
            //$query7 = "SELECT * from stores where address LIKE '%".$addressT."%'";
            $query7 = "SELECT * from stores where address LIKE '%".$addressT."%'";

            $data7 = mysqli_query($dbc,$query7);

            while($row7 = mysqli_fetch_array($data7)){

            $addressNew = explode(",", $row7['address']);
            $streetN = $addressNew[0];
            $cityN = $addressNew[1];
            $stateN = $addressNew[2];
            $zipN = $addressNew[3];
            $z_latitude = $row7['latitude'];
            $z_longitude = $row7['longitude'];

            $phone2 = distance($lat3, $lng3, $z_latitude, $z_longitude, "M");


                    echo '
                        <li><div 
                        data-type="store" 
                        class="store box"
                        data-latitude="'.$row7['latitude'].'" 
                        data-longitude="'.$row7['longitude'].'">

                        <a href="detail.php?id='.$row7['id'].'">
                        <p class="art">

                            <span class="title" data-type="title">'.$row7['name'].'</span><br>
                            <span data-type="address">'.$streetN.'</span><br>
                            <span data-type="city">'.$cityN.'</span>,<span data-type="state">'.$stateN.'</span><span data-type="zipcode">'.$zipN.'</span><br />
                            <span data-type="miles" style="font-size:10px;font-style:italic;">'.round($phone2,2).' miles away from you.</span>
                        </p>
                        </a>
                        </div></li>';




            }//ends while loop      
Manwal
  • 23,450
  • 12
  • 63
  • 93

1 Answers1

0

I would suggest to calculate the distance and sort rows in mysql, using tips from this other question: Fastest Way to Find Distance Between Two Lat/Long Points

But if you insist on solution within PHP, you would need to store all rows in array and then sort it using PHP usort() function http://php.net/manual/en/function.usort.php

Community
  • 1
  • 1
David162795
  • 1,846
  • 3
  • 15
  • 20