I am a current student and don't know much about where to even begin with this. :SI am working on a website for a friend and they want the visitor to be able to put in their zipcode and a list be generated with the nearest stores. Is this something that can be achieved with just PHP? Any help/coding is appreciated! I don't even know what the coding would look like for this...?? Thanks for your help!

7 Years
Discussion Span
Last Post by Stefano Mtangoo

PHP is ok. (for, basically, any job!)
If you have NO php experience, It will be painful, so I suggest you hire someone to do it.
If you have at least some PHP knowledge, check this out, it seems to be what you need for the job.


The way this is done, is using a database of zipcodes with geographic information attached, latitude and longitude.

Given a user's zipcode, its a matter of finding all zipcodes within a certain distance, say 10 miles, and then any store which has a location within those zipcodes.

Here is a great Scribd doc that explains it and shows the formulas as well as improvements and how to implement it within a mysql query: http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL

There are some free geolocation databases but I can't suggest any of them in particular or speak to their accuracy. Maybe someone else can point you to a really good one. There are a lot of commercial ones available if you have a budget.


mschroeder - I completely understand what you are saying. However, after looking at that provided link it looks very confusing!


You could avoid this and use this simple trick: a user provides the zip 12345. You then lookup for only for the 123xx matches in the database (the regional zip).
Get the idea ?


I disagree seeing as how that is not the way a zipcode is structured.

First digit is the state assignment
Second and Third are the regional processing facility (not the region)
Fourth and Fifth represent the town, or area within a large enough city.

Within a 10 mile radius of myself, there are 5 three digit zipcode prefixes.
within 20 miles you cross state lines and that number increases to 10 or more plus additional local zipcode prefixes.

Despite the geographic derivation of most ZIP codes, the codes themselves do not represent geographic regions; they generally correspond to address groups or delivery routes. Consequently, ZIP code "areas" can overlap, be subsets of each other, or be artificial constructs with no geographic area (such as 095 for mail to the Navy, which is not geographically fixed). Similarly, in areas without regular postal routes (rural route areas) or no mail delivery (undeveloped areas), ZIP codes are not assigned or are based on sparse delivery routes, and hence the boundary between ZIP code areas is undefined. For example, some residents in or near Haubstadt, Indiana, which has the zip code 47639, have mailing addresses with 47648, the zip code for neighboring Fort Branch, Indiana while others living in or near Fort Branch have addresses with 47639. Many rural counties have similar logistical inconsistencies caused by the aforementioned sparse delivery routes, often known as Rural Routes or by some other similar designation.



Yeah i have to agree with MrShoeder here.

I'm currently working on this problem as well ryan, only difference being that i'm doing it for Postcodes in the UK.

However none the less the logic should be the same.

From what I am aware of, you need the following parameters, longitude, latitude and the radius associated with any given zip code.

There is a calculation to determine the distance to get zip codes withing a certain range however i don't know that calculation yet.

I will let you know once i do.


Sounds good, thanks! Is this PHP only or will I need to deal with Mysql too?


PHP would be used to make the database connection and query the database, but the actual calculation can be done at the mysql server. With PHP processing the results and displaying them how you see fit.

There are a lot of examples covering this topic:

There are tons more but these looked like decent examples with explanations. Hope it helps.


Yes as Mschroeder stated, will be written in PHP but the calculation etc will be done using a mySQL query.

Thanks for the links Mschroader.

It may be a couple of weeks before i release the solution as i am currently working on other parts of the system, searching for venues within a specified range would be a nice feature but is not a necessary one and so will be one of the last things i do.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.