I need to show the result based on the calculations inside of a loop. The result of loop should be 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 the $distance?
It shows as:
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