PHP - output in ascending order while ()

I need to show the result based on the calculations inside the loop. The loop result should be in ascending order by $ distance.

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

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

    if ($result->num_rows > 0) {

        while($row = $result->fetch_assoc()) {



    $lat1 = $_GET['lat'];
    $lon1= $_GET['lon'];
    $lat2 = $row['latitude'];
    $lon2 = $row['longitude'];

    //starting calculating the distance

      $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;

    $distance = substr($unit,0,4);
echo $row['cinemaname'].$distance;

}}

How to show the result in ascending order based on $ distance?

It shows how:

cinema name 20 km
cinema name 5 km
cinema name 30 km
cinema name 3 km

I need to show in:

cinema name 3 km
cinema name 5 km
cinema name 20 km
cinema name 30 km
+4
source share
3 answers

Store the return value in an array like this:

$cinema[$i]['cinemaname'] = $row['cinemaname'];
$cinema[$i]['distance'] = $distance;

and after that do the following:

function sortByOrder($a, $b) {
    return $a['distance'] - $b['distance'];
}

usort($cinema, 'sortByOrder');

now you have an order :)

This is your code:

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

$result = $conn->query($sql);
$cinema = array();
$i = 0;
if ($result->num_rows > 0) {

    while($row = $result->fetch_assoc()) {
       $lat1 = $_GET['lat'];
       $lon1= $_GET['lon'];
       $lat2 = $row['latitude'];
       $lon2 = $row['longitude'];

       //starting calculating the distance

       $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;

       $distance = substr($unit,0,4);
       $cinema[$i]['cinemaname'] = $row['cinemaname'];
       $cinema[$i]['distance'] = $distance;
       $i++;
    }
}

function sortByOrder($a, $b) {
   return $a['distance'] - $b['distance'];
}

usort($cinema, 'sortByOrder');
+2
source
    $sql = "SELECT DISTINCT * FROM cinemas WHERE city='$city'";



       $result = $conn->query($sql);
    $distanceArray=array('');
$i=0;
        if ($result->num_rows > 0) {

        while($row = $result->fetch_assoc()) {



    $lat1 = $_GET['lat'];
    $lon1= $_GET['lon'];
    $lat2 = $row['latitude'];
    $lon2 = $row['longitude'];

    //starting calculating the distance

      $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;

    $distance = substr($unit,0,4);
    $distanceArray[$i]=$row['cinemaname'].$distance;
$i++;
}}
sort($distanceArray);
foreach($distanceArray as $nameDistance)
{
echo $nameDistance; echo '<br>';
}
0
source

MySQL . - :

$sql = "SELECT DISTINCT *, distance($lat1,$lon1) AS dist  FROM cinemas WHERE city='$city' ORDER BY dist ASC";

, MySQL.

0

Source: https://habr.com/ru/post/1652818/


All Articles