通过包含的函数对while循环进行排序?

时间:2014-11-19 04:04:20

标签: mysql

我正在处理一个SQL查询,我想通过一个我用来获取两点之间距离的函数对它进行排序。距离不是SQL查询的一部分,但我希望结果根据函数返回的内容以ASC顺序显示。这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

我建议使用其他问题的提示来计算mysql中的距离并对行进行排序:Fastest Way to Find Distance Between Two Lat/Long Points

但如果你坚持在PHP中使用解决方案,则需要将所有行存储在数组中,然后使用PHP usort()函数http://php.net/manual/en/function.usort.php

对其进行排序