Possible Duplicate:
mySQL select zipcodes within x km/miles within range of y
I have 2 tables 1st with post code and category and second with postcodes, latitude and longitude. Now i want to search ... hear is table format before i move to my question
Table1 (uk_data)
comp_post_code cat1 cat2
Table2 (uk_pc)
Postcode Latitude Longitude
Now I want to search cat1 and cat2 by cost code ... which i have made ...here it is
SELECT * from uk_data where
cat10 like :comp_post_code AND (
cat1 like :cat OR
cat2 like :cat)
This gives me result which is on this post code but Now i want to search items where are with in 5 mines and 10 miles of area how can i do that ?
I know how to calculate distance between postcodes but here is the code
function distance($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;
}
}
But dont know how can i get this in loop to chick Table 2 for get result in 5 or 10 miles ... please help