根据距离排列记录的 PHP 代码
Posted
技术标签:
【中文标题】根据距离排列记录的 PHP 代码【英文标题】:PHP code to arrange records as per distance 【发布时间】:2014-06-26 15:33:07 【问题描述】:我尝试了以下适用于旧数据库结构的方法
$lat = '-27.560263394765027';
$long = '153.08231055736542';
$query = "SELECT * , ((
ACOS( SIN( ".$lat." * PI( ) /180 ) * SIN( lat * PI( ) /180 ) + COS( ".$lat." * PI( ) /180 ) * COS( lat * PI( ) /180 ) * COS( (
".$long." - lon) * PI( ) /180 ) ) *180 / PI( )) *60 * 1.1515) AS `distance` FROM `km_stores` HAVING `distance` <=100 ORDER BY distance LIMIT 0 , 10";
而且,我正在尝试应用相同的原理,并在最大 100 公里半径内找到最近的 30 家商店,这次我的数据库结构完全不同,我需要运行一组 sql 查询来获得所需的输出。我的查询是
$select_resname = mysql_query("SELECT * FROM `wp_cf7dbplugin_submits` WHERE `form_name` = 'Add Store' AND `field_name` = 'store-name' LIMIT 0 , 30");
$select_resdesc = mysql_query("SELECT * FROM `wp_cf7dbplugin_submits` WHERE `form_name` = 'Add Store' AND `field_name` = 'store-description' LIMIT 0 , 30");
$select_reslat = mysql_query("SELECT * FROM `wp_cf7dbplugin_submits` WHERE `form_name` = 'Add Store' AND `field_name` = 'store-latitude' LIMIT 0 , 30");
$select_reslong = mysql_query("SELECT * FROM `wp_cf7dbplugin_submits` WHERE `form_name` = 'Add Store' AND `field_name` = 'store-longitude' LIMIT 0 , 30");
while(($fetch_resname = mysql_fetch_array($select_resname)) && ($fetch_resdesc = mysql_fetch_array($select_resdesc)) && ($fetch_reslat = mysql_fetch_array($select_reslat)) && ($fetch_reslong = mysql_fetch_array($select_reslong)))
// Print the output
在这种情况下,如何根据 GEO 位置排列记录?
【问题讨论】:
...你的数据库结构是什么? 听起来你想使用ORDER BY()
和/或GROUP BY()
@Fred-ii- 分组?我不这么认为。 !?!
【参考方案1】:
$select_resname = mysql_query("SELECT * FROM `wp_cf7dbplugin_submits` WHERE `form_name` = 'Add Store' AND `field_name` = 'store-name' LIMIT 0 , 30");
$select_resdesc = mysql_query("SELECT * FROM `wp_cf7dbplugin_submits` WHERE `form_name` = 'Add Store' AND `field_name` = 'store-description' LIMIT 0 , 30");
$select_reslat = mysql_query("SELECT * FROM `wp_cf7dbplugin_submits` WHERE `form_name` = 'Add Store' AND `field_name` = 'store-latitude' LIMIT 0 , 30");
$select_reslong = mysql_query("SELECT * FROM `wp_cf7dbplugin_submits` WHERE `form_name` = 'Add Store' AND `field_name` = 'store-longitude' LIMIT 0 , 30");
$resultAreas = array();
while(($fetch_resname = mysql_fetch_array($select_resname)) && ($fetch_resdesc = mysql_fetch_array($select_resdesc)) && ($fetch_reslat = mysql_fetch_array($select_reslat)) && ($fetch_reslong = mysql_fetch_array($select_reslong)))
$areaResult1 = @$fetch_resname["area_field"];
$areaResult2 = @$fetch_resdesc["area_field"];
$areaResult3 = @$fetch_reslat["area_field"];
$areaResult4 = @$fetch_reslong["area_field"];
if($areaResult1!="")
$resultAreas[]= $areaResult1;
if($areaResult2!="")
$resultAreas[]= $areaResult2;
if($areaResult3!="")
$resultAreas[]= $areaResult3;
if($areaResult4!="")
$resultAreas[]= $areaResult4;
sort($resultAreas);
print_r($resultAreas); // prints you the sorted result
【讨论】:
我认为这不会实时工作,因为它只是按照数字顺序排序,而不是按照距离排序。此外,将输出作为“数组”。为您的代码 +1。以上是关于根据距离排列记录的 PHP 代码的主要内容,如果未能解决你的问题,请参考以下文章