开发者

How to order by results from 2 separate tables in PHP and MySQL

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['开发者_StackOverflow社区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));


So the resulting array should be something like:

'name' => 'something',
'lat' => 'something',
'lng' => 'something',
'description' => 'something',
etc.

Use usort to sort it.

usort($array, 'sortByOption');
function sortByOption($a, $b) {
  $distA = dist($a['homelat'], $a['homelong'], $a['lat'], $a['long']);
  $distB = dist($b['homelat'], $b['homelong'], $b['lat'], $b['long']);
  return strcmp($distA, $distB);
}
function dist($x1, $y1, $x2, $y2) {
  return pow($x2 - $x1, 2) + pow($y2 - $y1, 2);
}

EDIT: Sorry, didn't 100% read your question. Revised for the solution (though not a great one). You should be calculating distance in the array itself. And to save time, don't do the square-root.

EDIT 2: Final result.

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'],
                          'dist'=>dist($aaa['homelat'], $aaa['homelon'], $row['lat'], $row['lng'])
    ));
  }
}
usort($points, 'sortByDist'); // This sorts the points!
echo json_encode(array("Locations"=>$points));
function sortByDist($a, $b) {
  return strcmp($a['dist'], $b['dist']);
}
function dist($x1, $y1, $x2, $y2) {
  return pow($x2 - $x1, 2) + pow($y2 - $y1, 2);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜