【问题标题】:PHP - get data from database compare it to variable and sort by less valuePHP - 从数据库中获取数据将其与变量进行比较并按较少的值排序
【发布时间】:2016-08-27 18:22:08
【问题描述】:

脚本应该:

  1. 使用 while() 从每个电影院的数据库中获取纬度和对数
  2. 比较用户的位置
  3. 以公里为单位获取输出
  4. 根据较少的公里数显示所有这些

一切都在下面的脚本中解决了,只有数字 4 没有实现。

我不知道如何比较 while() 循环的每个项目并按升序显示所有内容。

<?php

$sql = "SELECT DISTINCT * FROM cinemas WHERE city='$city'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {


$lat1 = $_GET['lat']; //latitude of a user
$lon1= $_GET['lon']; //longitude of a user
$lat2 = $row['latitude'];  //latitude of a cinema
$lon2 = $row['longitude']; //longitude of a cinema

  $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 = $miles * 1.609344; //calculated the distance between them

$distance = substr($unit,0,4)." km"; //output of a distance


  echo $distance;
   ?> 

      //here is the data about movie

<div id="time"><?php echo $row3['showone']; // timing of a movie ?></div>



}}
?>

脚本输出是:

20 km 
movie information

5 km
movie information

45 km 
movie information

1 km 
movie information

我需要按升序显示它们。

【问题讨论】:

  • 有很多问题展示了如何在 MySQL 中做到这一点。但是你想用 PHP 来做吗?
  • 在显示所有值之前,您需要将所有值存储到一个数组中。在输出之前有诸如 ksort 或 sort 之类的排序命令来执行此操作
  • @RickJames 好好解释一下,请在 mysql 中怎么做? Forbs 你能用代码描述一下吗

标签: php mysql location latitude-longitude


【解决方案1】:

使用sort() 表示您的距离。

echo sort($distance);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    • 1970-01-01
    • 2022-12-12
    • 1970-01-01
    相关资源
    最近更新 更多