我试图对一个有一个名为“经度”和“纬度”的变量的表进行排序。 我使用下面的代码对应给定的坐标(45,45)。 它正在对某些内容进行排序,但奇怪的是在我的列中水平排序,而不是行.....
$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");当我var_dump时,这就是结果(这是表格第一行的内容!!)让我疯了!!:
{ [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" }正确答案:
$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; }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; }最满意答案
你的距离函数没有看到$mylat , $mylong变量。 (你应该在函数体的第一行之前添加一个global $mylat, $mylong;
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.
更多推荐
发布评论