PHP - get data from database compare it to variable and sort by less value

允我心安 提交于 2019-12-12 04:37:36

问题


Script should:

  1. Take latitude and logitude from database of each cinema with while()
  2. Compare to location of a user
  3. Get the output in kilometers
  4. Show all of them based on less kilometers

Everything is solved on the script down below, only number 4 is not implemented.

I have no idea how to compare every item of while() loop and show everything in the ascending order.

<?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>



}}
?>

Script out is:

20 km 
movie information

5 km
movie information

45 km 
movie information

1 km 
movie information

I need to show them in ascending order.


回答1:


Use sort() with your distance.

echo sort($distance);


来源:https://stackoverflow.com/questions/39184358/php-get-data-from-database-compare-it-to-variable-and-sort-by-less-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!