【问题标题】:Get Locations Nearest to Longest Using Php?使用 PHP 获取最近到最长的位置?
【发布时间】:2012-02-06 14:27:21
【问题描述】:

嗨,在我的数据库中,我总共有 100 个位置,这 50 个位置离我最近,另外 50 个位置对我来说最长,所以我想使用这个顺序对数据进行排序,所以我在这里计算距离

$latt=$_REQUEST['latt'];
$long=$_REQUEST['long'];

$start=$_REQUEST['start'];
 function distancefind($lat1, $lon1, $lat2, $lon2, $unit) { 

  $theta = $lon1 - $lon2; 
  $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); 
  $dist = acos($dist); 
  $dist = rad2deg($dist); 
  $miles = $dist * 60 * 1.1515;
  $unit = strtoupper($unit);

  if ($unit == "K") {
    return ($miles * 1.609344); 
  } else if ($unit == "N") {
      return ($miles * 0.8684);
    } else {
        return $miles;
      }
}



 $c=0;
    $limt=10;
    if($start!=null)
    {
    $query="select * from  tbl_MapDetails LIMIT $start,$limt";
    }
$result=mysql_query($query);
if($result)
{
while($row=mysql_fetch_array($result))
{

$distance1=distancefind($latt,$long,$row['Latitude'],$row['Longitude'],"");


$message[$c]=array("ID"=>$row['LocationID'],"Address"=>$address,"City"=>$row['City']=($row['City'] != null)?$row['City']:"","State"=>$row['State']=($row['State'] !=null)?$row['State']:"","Country"=>$row['Country']=($row['Country']!=null)?$row['Country']:"","Zip"=>$row['Zip']=($row['Zip'] !=null)?$row['Zip']:"","Country"=>$row['Country']=($row['Country']!=null)?$row['Country']:"","Distance"=>$distance1=($distance1==null)?"0":$distance1,"Latitude"=>$row['Latitude']=($row['Latitude']!=null)?$row['Latitude']:"","Longitude"=>$row['Longitude']=($row['Longitude']!=null)?$row['Longitude']:"","Pic"=>$pic,"rating"=>$row1[0]=($row1[0]!=null)?$row1[0]:"","name"=>$row['LocationName']=($row['LocationName']!=null)?$row['LocationName']:"","note"=>$row['Note']=($row['Note']!=null)?$row['Note']:"","feature1"=>$row['FeatureIcon1']=($row['FeatureIcon1']!=null)?$row['FeatureIcon1']:"","feature2"=>$row['FeatureIcon2']=($row['FeatureIcon2']!=null)?$row['FeatureIcon2']:"","feature3"=>$row['FeatureIcon3']=($row['FeatureIcon3']!=null)?$row['FeatureIcon3']:"","selectLogo"=>$row['SelectIcon']=($row['SelectIcon']!=null)?$row['SelectIcon']:"");
$c++;
}

所以每次我得到 10 个位置时我都会得到,所以消息数组数据从最近到最长开始我该如何检查请指导我

感谢提前

【问题讨论】:

标签: php mysql


【解决方案1】:

如果你想进行排序first和限制second(即选择$limt最接近的东西(偏移$start)到($lat,$long)) ,您可以在 MySQL 中完成:

$query = "SELECT *,
 ((ACOS(  SIN($lat*PI()/180)*SIN(Latitude*PI()/180) 
        + COS($lat*PI()/180)*COS(Latitude*PI()/180)*COS(($long-Longitude)*PI()/180
        )
  ) * 180/PI()
 )*60 * 1.1515) AS dist
FROM  tbl_MapDetails
ORDER BY dist 
LIMIT $start,$limt";

但是,如果您想按距离进行 $limt firstthen 排序(所以选择 $limt 项目然后按距离排序 - 可能不会包含最近的位置),使用@SergeyRatnikov的答案,或者你需要做一个嵌套选择:

SELECT * from
 (SELECT *,
   ...[distancecalculation]... AS dist
   FROM tbl_MapDetails
   LIMIT $start,$limt)
ORDER BY dist

【讨论】:

  • 执行此查询,但它不起作用.. 我得到 #1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 'PI()/180)*SIN(LatitudePI()/180) + COS(PI() 附近使用的正确语法/180)*COS(纬度PI()/180)*COS((8'在第1行
  • 当我在 mysql 提示符中键入它时,这对我有用。你有不平衡的括号之类的吗?您可以尝试逐位构建查询(即首先执行SIN($lat*PI()/180),然后逐个添加每个表达式)并查看它出错的步骤?
【解决方案2】:

此代码按距离对数组 $message 进行排序

function cmp($a, $b) {
    if ($a["Distance"] == $b["Distance"]) {
        return 0;
    }
    return (floatval($a["Distance"]) < floatval($b["Distance"])) ? -1 : 1;
}

uasort($message, 'cmp');

【讨论】:

  • 尝试usort 而不是uasort。此函数将新键分配给数组中的元素。它将删除任何可能已分配的现有键,而不仅仅是重新排序键。
猜你喜欢
  • 2016-02-09
  • 1970-01-01
  • 1970-01-01
  • 2012-11-15
  • 2020-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多