mysql - PHP - output result in ascending order of while() loop -


i need show result based on calculations inside of loop. result of loop should ascending order $distance.

    $sql = "select distinct * cinemas 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 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 show result in ascending order based on $distance?

it shows as:

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

i need show in:

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

save return in while in array this:

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

and make after while this:

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

now have order :)

this code now:

$sql = "select distinct * cinemas 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 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'); 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -