PHP - 从数据库获取数据将其与变量进行比较,并按较小的值排序

时间:2016-08-27 18:22:09

标签: php mysql location latitude-longitude

脚本应该:

  1. 使用while()
  2. 从每个电影院的数据库中获取纬度和经度
  3. 与用户的位置比较
  4. 以千米为单位获取输出
  5. 以较少的公里数显示所有这些
  6. 所有内容都在下面的脚本中解决,只有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
    

    我需要按升序显示它们。

1 个答案:

答案 0 :(得分:0)

与您的距离使用sort()

echo sort($distance);