3

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

Community
  • 1
  • 1
Harinder
  • 1,257
  • 8
  • 27
  • 54
  • Are you asking ["how to get data from multiple tables"](http://stackoverflow.com/questions/12475850/how-can-an-sql-query-return-data-from-multiple-tables), how to filter the resulting data, or something else? – DCoder Dec 30 '12 at 08:40
  • @DCoder I want to know how to get and filter data and get the result with in 5 miles of the result ... like if some one want to get result of cat1(book shop) near postcode AB10 1XN he should get result of all book shop near this post code within 5 miles – Harinder Dec 30 '12 at 08:44

1 Answers1

0

I think similar question is here.

Not sure but something you can find inside PHP also similar to Geokit gem of RoR

Community
  • 1
  • 1
Raghvendra Parashar
  • 3,883
  • 1
  • 23
  • 36