PDA

View Full Version : Zip Code Radius Search


Jim7283
05-13-2008, 02:53 PM
Hi All,

Our user database has grown to over 150,000 and in an attempt to enhance the current user search feature in the admin area we would like to be able to pull lists of people via zip code radius.

You've probably seen this functionality everywhere on the web - just plug in the zip code and check an option for 5,10,25 etc. miles and it returns the right listing.

Does anyone know how this is done, and if it can be replicated using PHP? Is there some sort of zip code database that is available to compare our users zip codes against? How is the distance between zip codes calculated?

I'm about to start researching this now, but figured I would post here as well in case anyone else has done something like this...

Jim7283
05-13-2008, 03:00 PM
Well here's step 1 - get a zip code database http://www.populardata.com/downloads.html

Step 2 is to use a class to handle the distance calculation, and I'm trying a few from http://www.phpclasses.org I'll report back once I have found one that works well...

Jim7283
05-21-2008, 11:04 AM
Got this working well - here's everything you need to get it going:

1. Zip Code Database - I've taken the time to clean-up/normalize the data, and you can get a copy of the .CSV which contains all 2006 US Zip Codes from my site at http://doyledesigninc.com/upload/zips.csv

2. Zip Code Calculation Class - there are a number of them available, but I found Micah Carrick's was the easiest to implement http://www.micahcarrick.com/04-19-2005/php-zip-code-range-and-distance-calculation.html

3. Then you just need to add the zipcodes table to your database, and adjust the queries in the zip code class to match the table names in your DB. The class has a number of great functions for pulling information about the zip codes, but the only one I needed was:


function get_zips_in_range($zip, $range, $sort, $include_base)


Hope that helps anyone else looking to add similar functionality!

Vege
05-21-2008, 04:30 PM
Some countrys have zip codes, some don't.
Never force people to enter their zip code if you plan to go multinational :)

Jim7283
05-22-2008, 09:46 AM
Understood - but as I stated this is only a US zip code database, and is for use in the back-end admin area of a company site, not for use by our users.