简体   繁体   English

从数组对数据库排序

[英]Sorting a database from an array

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. 很显然,最底层的阵列是基于仅发现的3家企业。.一次可能有几家企业。 I just don't know how to adjust that automatically? 我只是不知道如何自动调整? the $distance1 code below works great. 下面的$ distance1代码效果很好。 $distance1 does show correct distances for all businesses rendered. $ distance1确实显示了所有呈现的业务的正确距离。 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 ////

 }

You need usort . 你需要usort

have a look at this post https://stackoverflow.com/a/2699159/2209160 看看这个帖子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 如果您的PHP版本> = 5.3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM