57208

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

Question:

Script should:

<ol><li>Take latitude and logitude from database of each cinema with while()</li> <li>Compare to location of a user</li> <li>Get the output in kilometers</li> <li>Show all of them based on less kilometers</li> </ol>

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.

Answer1:

Use sort() with your distance.

echo sort($distance);

Recommend

  • Sort by embedded document
  • Initial DataGrid Sorting
  • Pandas Dataframe ordering and sorting of column values
  • NSFetchedResultsController doesn't always call didChangeObject:atIndexPath:forChangeType:newInd
  • Delete object in core data, failed to match the Swift Array Element type
  • Ordering a Union Query in MS Access SQL
  • Taking mean across rows grouped by a variable in numpy
  • Grouping by blank nodes
  • How to sort things out in ListView?
  • Crafting a LINQ based solution to determine if a set of predicates are satisfied for a pair of colle
  • Query to get the Top 2 from each group
  • How to implement limit with Nhibernate and Sybase
  • pyodbc doesn't report sql server error
  • Mysterious problem with floating point in LISP - time axis generation
  • How to know when stdin is empty if it contains EOF?
  • Breeze - Deleted Items nav properties bug
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • PHP - How to update data to MySQL when click a radio button
  • Counter field in MS Access, how to generate?
  • javaw.exe and eclipse startup problems
  • Sending data from AppleScript to FileMaker records
  • Upload files with Ajax and Jquery
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • ORA-29908: missing primary invocation for ancillary operator
  • Do I've to free mysql result after storing it?
  • How to get next/previous record number?
  • A cron job substitute?
  • json Serialization in asp
  • KeystoneJS: Relationships in Admin UI not updating
  • AngularJs get employee from factory
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Load html files in TinyMce
  • How do you join a server to an Active Directory (domain)?
  • need help with bizarre java.net.HttpURLConnection behavior
  • How does Linux kernel interrupt the application?
  • costura.fody for a dll that references another dll
  • Reading document lines to the user (python)
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal