views:

185

answers:

4

I'm aware there are databases for zip codes, but how would I grab the city/state fields based on that? Do these databases contain the city/states or do I have to do some sort of lookup to a webservice?

+1  A: 

Modern zip code databases contain columns for City, State fields.

http://sourceforge.net/projects/zips/

http://www.populardata.com/

Pentium10
That's nice, I'm sure most figured this. Perhaps an example of where to find said databases, bonus points for open source, would make this a real answer.
Urda
Just search StackOverflow and you will locate for example this: http://stackoverflow.com/questions/24471/zip-code-database check out the second option
Pentium10
@Pentium10, I saw the SourceForge one, but the last update was in 2005. Can't we consider that too old to be useful?
Urda
You have a point. If your project does need better accuracy you have to buy a recent one. For a startup website the older databases are just fine. I had no complaints from my clients.
Pentium10
+2  A: 

\begin{been-there-done-that}

Important realization: There is not a one-to-one mapping between cities/counties and ZIP codes. A ZIP code is not based on a political area but instead a distribution area as defined for the USPS's internal use. It doesn't make sense to look up a city based on a ZIP code unless you have the +4 or the entire street address to match a record in the USPS address database; otherwise, you won't know if it's RICHMOND or HENRICO, DALLAS or FORT WORTH, there's just not enough information to tell.

This is why, for example, many e-commerce vendors find dealing with New York state sales tax frustrating, since that tax scheme is based on county, e-commerce systems typically don't ask for the county, and ZIP codes (the only information they provide instead) in New York can span county lines.

The USPS updates its address database every month and costs real money, so pretty much any list that you find freely available on the Internet is going to be out of date, especially with the USPS closing post offices to save money.

One ZIP code may span multiple place names, and one city often uses several (but not necessarily whole) ZIP codes. Finally, the city name listed in the ZIP code file may not actually be representative of the place in which the addressee actually lives; instead, it represents the location of their post office. Our office mail is addressed to ASHLAND, but we work about 7 miles from the town's actual political limits. ASHLAND just happens to be where our carrier's route originates from.

For guesstimating someone's location, such as for a search of nearby points of interest, these sources and City/State/ZIP sets are probably fine, they don't need to be exact. But for address validation in a data entry scenario? Absolutely not--validate the whole address or don't bother at all.

Just a friendly reminder to take a step back and remember the data source's intended use!

\end{been-there-done-that}

Nicholas Piasecki
More specifically the feature I'm implementing is just a layer of Javascript and it would guestimate the location and populate the textfields if they aren't already - the user would still have to verify that the information is correct before submitting it, so we're not totally relying on the zip code.
meder
A: 

I'll try to answer the question "HOW should I populate...", and not "SHOULD I populate..."

Assuming you are going to do this more than once, you would want to build your own database. This could be nothing more than a text file you downloaded from any of the many sources (see Pentium10 reply here). When you need a city name, you search for the ZIP, and extract the city/state text. To speed things up, you would sort the table in numeric order by ZIP, build an index of lines, and use a binary search.

If you ZIP database looked like (from sourceforge):

"zip code", "state abbreviation", "latitude", "longitude", "city", "state"
"35004", "AL", " 33.606379", " -86.50249", "Moody", "Alabama"
"35005", "AL", " 33.592585", " -86.95969", "Adamsville", "Alabama"
"35006", "AL", " 33.451714", " -87.23957", "Adger", "Alabama"

The most simple-minded extraction from the text would go something like

$zipLine = lookup($ZIP);
if($zipLine) {
    $fields = explode(", ", $zipLine);
    $city  = $fields[4];
    $state = $fields[5];
}  else {
    die "$ZIP not found";
}

If you are just playing with text in PHP, that's all you need. But if you have a database application, you would do everything in SQL. Further details on your application may elicit more detailed responses.

gary
A: 

USPS ZIP tables change monthly. Most free/public ZIP tables aren't reliable. For cheap current validation, see http://semaphorecorp.com

joe snyder