我目前有此代码将所有信息保存到数据库中,但我希望能够允许用户使用他们的邮政编码进行搜索,以便仅显示他们选择的半径内的结果,例如。 1英里,2英里等 我不确定如何做到这一点。
<form action="addhobby.php" method="POST">
Type:<input type="radio" name="type" value="meetup" checked>Meetup
<input type="radio" name="type" value="chat" checked>Chat
<br />
Hobby:<input type="text" name="hobby" value=""><br />
Location:<input type="text" name="location" value=""><br />
Postcode:<input type="text" name="postcode"><br />
Starts: <input type="date" name="startdate"> <input type="time" name="starttime"><br />
Ends: <input type="date" name="enddate"> <input type="time" name="endtime"> <br />
Description: <input type="text" name="description"><br />
<input type="submit" name="submit" value="Create">
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$sql="INSERT INTO activities (hobby, location, postcode, sdate, stime, edate, etime, description, type) VALUES
('$_POST[hobby]','$_POST[location]', '$_POST[postcode]', '$_POST[startdate]', '$_POST[starttime]', '$_POST[enddate]', '$_POST[endtime]', '$_POST[description]', '$_POST[type]' )";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
header("location:profile.php");
mysql_close($con)
?>
如果您可以使用HTML5地理位置API获取纬度和经度,为什么要使用邮政编码?
您可以使用以下 SQL 语句:
SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 5 ORDER BY distance;
必须将lat
替换为纬度,将lng
替换为经度。要按公里而不是英里进行搜索,请将 3959 替换为 6371。
来源: https://developers.google.com/maps/articles/phpsqlsearch_v3
为此,
您需要一个邮政编码数据库,该数据库为每个邮政编码提供纬度和纵向坐标。如果您有此数据,则可以查询位于指定距离之间的所有邮政编码。
您可以使用这样的开源邮政编码数据库(仅适用于美国)。