简体   繁体   English

查询后的PHP mySQL订单结果

[英]PHP mySQL Order results after query is made

I have a database which contains postcodes along with their geo coordinates. 我有一个数据库,其中包含邮政编码及其地理坐标。 The data looks similar to the following 数据类似于以下内容

 id | Postcode   | Longitude  |  Latitude      |  
-------------------------------------------------
 1  | W12 7GF    | 51.51527   |  -0.08816      |  
-------------------------------------------------
 2  | SW16 6GF   | 51.51528   |  -0.15960      |  
-------------------------------------------------
 3  | W1 4FT     | 51.51528   |  -0.11590      | 
-------------------------------------------------

What I first do is take in a postcode (I have hardcoded it for now). 我首先要做的是输入邮政编码(我现在已经对其进行了硬编码)。

$sql = "SELECT * FROM postcodes WHERE `postcode` = 'W14 6TY'";

Once I execute this, I get the Longitude and Latitude for that postcode. 执行此操作后,将获得该邮政编码的经度和纬度。 I also set a couple more variables. 我还设置了另外两个变量。

$lat1 = $row['Latitude'];
$lon1 = $row['Longitude'];
$d = 5;
$r = 3959;

Now what I do is get all other postcodes within a 5 mile radius of the above postcode. 现在,我要做的是在上述邮政编码的5英里半径内获取所有其他邮政编码。 To do this, I do 为此,我要做

$latN = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(0))));
$latS = rad2deg(asin(sin(deg2rad($lat1)) * cos($d / $r) + cos(deg2rad($lat1)) * sin($d / $r) * cos(deg2rad(180))));
$lonE = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(90)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));
$lonW = rad2deg(deg2rad($lon1) + atan2(sin(deg2rad(270)) * sin($d / $r) * cos(deg2rad($lat1)), cos($d / $r) - sin(deg2rad($lat1)) * sin(deg2rad($latN))));

$query = "SELECT * FROM postcodes WHERE (Latitude <= $latN AND Latitude >= $latS AND Longitude <= $lonE AND Longitude >= $lonW) AND (Latitude != $lat1 AND Longitude != $lon1)  ORDER BY Latitude, Longitude ASC LIMIT 30";
$result2 = $conn->query($query);

As you can see I limit the results because I do not want hundreds returned. 如您所见,我限制结果,因为我不希望返回数百个结果。 Lastly, I output the data 最后,我输出数据

echo "<div class='container'>";
    echo "<div class='row'>";
        echo "<div class='col-md-12 col-sm-12 col-xs-12'>";
            echo "<table class=\"table table-striped\">";
                echo "<tr><th>Postcode</th><th>Latitude</th><th>Longitude</th><th>Miles, Point A To B</th></tr>\n";
                    while ($row = $result2->fetch_assoc()) {
                        echo "<tr><td>$row[Postcode]</td><td>$row[Latitude]</td><td>$row[Longitude]</td>";
                        echo "<td>".acos(sin(deg2rad($lat1)) * sin(deg2rad($row['Latitude'])) + cos(deg2rad($lat1)) * cos(deg2rad($row['Latitude'])) * cos(deg2rad($row['Longitude']) - deg2rad($lon1))) * $r."</td>";
                        echo "</tr>\n";
                    }
                echo "</table>\n<br />\n";
        echo "</div>";
    echo "</div>";
echo "</div>";

As you can see in the output, I also calculate the Miles, Point A To B. I cant do this at a database query level because I need to do all that maths on the resulting geo-coordinates. 如您在输出中看到的,我还计算了里程,点A到B。我无法在数据库查询级别上执行此操作,因为我需要对生成的地理坐标进行所有这些数学运算。

At the moment, the data is ordered by Latitude and Longitude. 目前,数据按纬度和经度排序。 Because these numbers do not make much sense, the output looks a bit funny. 因为这些数字没有太大意义,所以输出看起来有点可笑。

My question is, would it be possible to order the output based on the smallest to highest number of miles between points? 我的问题是,是否可以根据点之间的最小到最大里程数来排序输出? I presume I would need to remove the limit (so it can work on all outputs), but not sure if I can do this because I do not calculate this until after the query. 我想我需要删除该限制(以便它可以在所有输出上使用),但是不确定是否可以执行此操作,因为在查询之后才进行计算。

Any advice appreciated. 任何建议表示赞赏。

Thanks 谢谢

Eg 例如

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,Postcode VARCHAR(12) NOT NULL
,Longitude DECIMAL(8,5)
,Latitude DECIMAL(8,5) NOT NULL
);

INSERT INTO my_table VALUES
(1  ,'W12 7GF',51.51527,-0.08816),
(2  ,'SW16 6GF',51.51528,-0.15960),
(3  ,'W1 4FT',51.51528,-0.11590),
(4  ,'W14 8UX',51.49645,-0.20975);

SELECT y.*
     , ROUND(geo_distance_km(x.latitude,x.longitude,y.latitude,y.longitude),2) dist 
  FROM my_table x 
  JOIN my_table y 
    ON y.id <> x.id 
 WHERE x.postcode = 'W14 8UX';
+----+----------+-----------+----------+-------+
| id | Postcode | Longitude | Latitude | dist  |
+----+----------+-----------+----------+-------+
|  1 | W12 7GF  |  51.51527 | -0.08816 | 13.69 |
|  2 | SW16 6GF |  51.51528 | -0.15960 |  5.96 |
|  3 | W1 4FT   |  51.51528 | -0.11590 | 10.65 |
+----+----------+-----------+----------+-------+

Obviously, I've left a crucial bit out of this answer. 显然,我在这个答案中遗漏了至关重要的一点。 I wonder if you can figure that bit out for yourself. 我想知道您是否能自己解决这个问题。

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

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