0

I am trying to sort a table that has a variable called "longitude" and "latitude". I use the code below against a given coordinate (45,45). It is sorting something, but it's strangely sorting horizontally across my columns, not the row.....

$mylat = 45;
$mylong= 45;

$res = mysql_query("SELECT * FROM account");
$users = mysql_fetch_row($res);

function distance($lon1, $lat1) { 
    $theta = $lon1 - $mylong; 
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($mylat)) +  cos(deg2rad($lat1)) * cos(deg2rad($mylat)) * cos(deg2rad($theta)); 
    $dist = acos($dist); 
    $dist = rad2deg($dist);
    return $dist;
}

function cmp($a, $b) {
    $distA = distance($a['longitude'],$a['latitude']);
    $distB = distance($b['longitude'],$b['latitude']);
    if($distA == $distB) {
    return 0;
}
    return ($distA > $distB) ? -1 : 1;
}

usort($users, "cmp");

When I var_dump, this is the results (which are the content of the first row of the table!!) Driving me mad!!:

 { [0]=> string(2) "30" [1]=> string(3) "999" [2]=> string(14) "20131018111824" [3]=> string(2) "30" [4]=> string(1) "m" [5]=> string(8) "xxxxxxxx" [6]=> string(8) "xxxxxxxx" [7]=> string(2) "45" [8]=> string(12) "xxx@gmail.com" }

CORRECT ANSWER:

$res = mysql_query("SELECT * FROM account");
   while( $row = mysql_fetch_assoc( $res)){
      $users[] = $row; // Inside while loop
   }

function distance($lon1, $lat1) { 
    GLOBAL $mylat;
    GLOBAL $mylong;
    $theta = $lon1 - $mylong; 
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($mylat)) +  cos(deg2rad($lat1)) * cos(deg2rad($mylat)) * cos(deg2rad($theta)); 
    $dist = acos($dist); 
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515; 
    return $miles;
}
minjiera
  • 336
  • 3
  • 16
  • 2
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). – h2ooooooo Oct 18 '13 at 10:33
  • `$mylong` and `$mylat` aren't in scope for the distance() function; 2nd line of the cmp() function should set `$distB`, not `$distA`; you're only fetching one row from your SQL resultset, so that's an array based on columns; so you're actually trying to sort each column of that single result; not an array of rows – Mark Baker Oct 18 '13 at 10:34
  • thanks for catching the 2 errors. But the outcome is still the same. I am strangely getting my first row's variables sorted @____@ – minjiera Oct 18 '13 at 10:37
  • @h2ooooooo thanks...I am quite new. Guess I need to be more selective of my reference. But this should not create the problem I am experiencing, or? – minjiera Oct 18 '13 at 10:38
  • Where does `$theta`come into play? I don't see it defined anywhere... – ErnestV Oct 18 '13 at 10:43
  • it's defined "$theta = $lon1 - $mylong; " , and used in the next line :) – minjiera Oct 18 '13 at 10:49
  • I figured it's the way I am querying...something's not right with my $users array to begin with....but I don't know what. :( – minjiera Oct 18 '13 at 10:49
  • 1
    Get the full list of users before sorting: `$users = array(); while ($user = mysql_fetch_row($res) { $users[] = $user; }` – Mark Baker Oct 18 '13 at 10:50
  • But if you're working with large numbers of account records, this is not going to be very efficient.... look at using a bounding box to reduce the work by selecting only relatively close account records in the first place - http://stackoverflow.com/questions/5236921/geo-search-distance-in-php-mysql-performance/5238667#comment28689275_5238667 – Mark Baker Oct 18 '13 at 10:52

1 Answers1

0

Your distance function doesn't see the $mylat, $mylong variables. (You should add a global $mylat, $mylong; before the first line of the function body.

Lajos Veres
  • 13,595
  • 7
  • 43
  • 56