Reputation: 31
I currently have this code which saves all the information to the database but I want to be able to allow users to search using their postcode so only results within the radius they select is shown eg. 1mile, 2mile etc I am not sure how to go about doing this.
<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)
?>
Upvotes: 0
Views: 474
Reputation: 373
Why using postcode if you can get latitude and longitude with HTML5 Geolocation API?
You can use the following SQL statement:
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;
You must replace lat
with a latitude and lng
with a longitude. To search by kilometers instead of miles, replace 3959 with 6371.
Source: https://developers.google.com/maps/articles/phpsqlsearch_v3
Upvotes: 2
Reputation: 1164
To do this you will need a zip code database which provides latitude and longtitude coordinates for each zip code. If you have this data, then you can query all the ZIP codes which are located between a specified distance.
You can use an open source zip code database like this (for US only).
Upvotes: 0