PHP,MYSQL存储定位器,带有距离邮政编码纬度经度计算 [英] PHP, MYSQL store locator with distance postcodes latitude longitude calculations

查看:95
本文介绍了PHP,MYSQL存储定位器,带有距离邮政编码纬度经度计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对php和mysql相当新,下面的php代码计算两个给定邮政编码之间的距离并返回距离作为输出。



我想要实现的是能够获得数据库表中与$ secondPC具有相同值的所有邮政编码的距离值,尽管它们的纬度和经度值可能不同。



任何帮助都将非常感谢。



I am fairly new to php and mysql, the php code below calculates the distance between two given postcode and returns the distance as output.

What I am trying to achieve is to be able to get distance values for all postcodes within the database table that have the same value as $secondPC, although their latitude and longitude values maybe different.

Any help will be most appreciated thanks.

$dbName=" ";
$dbUsername=" ";
$dbPassword=" ";
 

$fromPC=$_POST['fromPC'];
$toPC=$_POST['toPC'];
 

// 
 
function getDistance($lat1, $long1, $lat2, $long2, $unit)
{

if($unit=="miles"){
$earth = 3960; //miles
}else{
$earth = 6371; //kilometres
}
 
//From co-ordinates
$lat1 = deg2rad($lat1);
$long1= deg2rad($long1);
 
//To co-ordinates
$lat2 = deg2rad($lat2);
$long2= deg2rad($long2);
 
// The Haversine Formula
$dlong=$long2-$long1;
$dlat=$lat2-$lat1;
 
$sinlat=sin($dlat/2);
$sinlong=sin($dlong/2);
 
$a=($sinlat*$sinlat)+cos($lat1)*cos($lat2)*($sinlong*$sinlong);
 
$c=2*asin(min(1,sqrt($a)));
 
$d=round($earth*$c);
 
return $d;
}
 

 

 
if( (!empty($fromPC)) && (!empty($toPC)) )
{
mysql_connect(localhost,$dbUsername,$dbPassword);
@mysql_select_db($dbName) or die( "Unable to select database");
 
// basic cleaning of input
$firstPC = strtoupper(preg_replace("/[^a-zA-Z0-9]/","",$fromPC ));
$secondPC = strtoupper(preg_replace("/[^a-zA-Z0-9]/","",$toPC ));
 
// get first details
$query = 'SELECT `latitude`, `longitude` FROM `uk_postcodes` WHERE `postcode`="'.$firstPC.'";';
$result = mysql_query($query);
$first = mysql_fetch_row($result);
$checkFirst=mysql_num_rows($result);
 
// get second details
$query = 'SELECT `latitude`, `longitude` FROM `uk_postcodes` WHERE `part_type` LIKE "'.$secondPC.'";';
$result = mysql_query($query);
$second = mysql_fetch_row($result);
$checkSecond=mysql_num_rows($result);
 

 

// ensure there were results to calculate with
if( ($checkFirst<1) || ($checkSecond<1) ){
$outputResults="Unrecognised postcode entered.";
}else{
$distance = getDistance($first[0], $first[1], $second[0], $second[1], "miles");
$outputResults = "Nearest Store is: ".$distance." miles from you";
$term = "$toPC";
$qry = ("SELECT $first[0],$first[1],$second[0],$second[1], id FROM uk_postcodes WHERE part_type LIKE '%$term%'");
$res = mysql_query($qry);
function mysql_fetch_all($res) {
while($row=mysql_fetch_array($res)) {
$return[] = $row;
}
return $return;
}
 
function create_table($dataArr) {
echo "";
for($j = 0; $j < count($dataArr); $j++) { 
echo "".$dataArr[$j]."";
}
echo "";
}
 
$all = mysql_fetch_all($res);
 
echo "";
 
for($i = 0; $i < count($all); $i++) {
create_table($all[$i]);
echo $all[$i];
echo $all[$i];
}
 
echo "
";
$dist = getDistance($first[0], $first[1], $all[$i][0], $all[$i][1], "miles");
echo $dist;
 
}

推荐答案

secondPC,虽然它们的纬度和经度值可能不同。



任何帮助都将非常感谢。



secondPC, although their latitude and longitude values maybe different.

Any help will be most appreciated thanks.


dbName = ;
dbName=" ";


dbUsername = < span class =code-string> ;
dbUsername=" ";


这篇关于PHP,MYSQL存储定位器,带有距离邮政编码纬度经度计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆