I want to add a service to my site where a visitor can enter their postcode (or zipcode) and a list of other members in their area are displayed - with those that share the same code appearing first and then those that are close by etc... The first part can be done using a pretty straight forward search function (i'm sure!), its the second part where a list of those in neighbouring postcodes appear that i am having difficulties with - for example if i live in sw4 then how can i show all of those who live in in sw5, sw3 etc? Does anyone have any idea how to go about this? or know of any resources that could point me in the right direction??
You can not do that only with zip code, you need a zip code database that has latitude and longitute information of each zipcode; then you can find people with any radius from your own zip code.
There are *some* free resources on my page here (also some that are not free): zipwise.com/web-tools.php
There's plenty of databases you can buy that have post code area centroids (lat/lon). Then with Pythagoras' theorem you can calculate distances. Here's an intro which should get you on your way... Here is such a UK database and this company sells both the database as well as a set of handy PHP functions which do it all for you.
The way I have done this is as follows: When a user signs up I use php to query www.postcodeanywhere.co.uk and they return an xml file showing the latitude and longitude of the users postcode. This is then saved to the database with the users details. You can then enter any postcode and query www.postcodeanywhere.co.uk again to find out the latitude and longitudes and then use Pythagoras to compare them and work out the seperation distance. Each query costs around 4 pence and I have the php scripts if you want. see www.noblemarine.co.uk/repairers.php3 for an example.
i don't think this should be easy unless you have a datbase or an algorithm that tells you the distance between to zip codes
Once you know the coordinates of each postcode/zipcode the distance between them is very easy to calculate. $dist1 = abs($easting1 - $easting2); $dist2 = abs($northing1 - $northing2); $dist1 = $dist1^2; $dist2 = $dist2^2; $distance_in_metres = $dist1 + $dist2; $distance_in_metres = $distance_in_metres^0.5; $distance_in_miles = $distance_in_metres/1609; $distance_in_miles = number_format($distance_in_miles, 1); PHP: