How to order by results from 2 seperate tables in PHP and MySQL.
- by Vafello
I am trying to output results of 2 sql queries to one JSON file. The problem is that I would like to order them ascending by distance which is the result of equation that takes homelat and homelon from the users table and lat, lng from locations table.(basically it takes lattitude and longitude of one point and another and computes the distance between these points). Is it possible to take some parameters from both select queries, compute it and output the result in ascending order?
$wynik = mysql_query("SELECT homelat, homelon FROM users WHERE guid='2'") or
die(mysql_error()); ;
$query = "SELECT * FROM locations WHERE timestamp";
$result = map_query($query);
$points = array();
while ($aaa = mysql_fetch_assoc($wynik)) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
array_push($points, array('name'=>$row['name'], 'lat'=>$row['lat'],
'lng'=>$row['lng'], 'description'=>$row['description'],
'eventType'=>$row['eventType'], 'date'=>$row['date'],
'isotime'=>date('c', ($row['timestamp'])), 'homelat'=>$aaa['homelat'],
'homelon'=>$aaa['homelon']));
}
echo json_encode(array("Locations"=>$points));