How to Code a Simple Zip Code Locator in PHP
Why pay hundreds of dollars for a commercial zip code locator when you can make your own in a few hours? This post is a start to finish tutorial on how to make a simple zip code locator using PHP, MySQL, a free zip database, and the Yahoo Maps API. Here’s a demo if you want to see it first.
Let’s get started:
1. Requirements
- You’ll need PHP and MySQL running on your webserver
- You need a zip code database. You can find a free one from the 2000 census here, or you can purchase one. Using the free one is good for getting started, but it is quite out of date.
- You’ll need a Yahoo Maps API key, which we’ll get to later.
2. Get the Zip Database
Download the zip database.
It comes in CSV or SQL format. Download the SQL version (it’s under zipcodedb-MySQLdump).
14,978 views | 5 Comments »
Related Posts
- October 2007 Recap and Traffic Report
- How to Make a Resizeable Full Screen Flash Animation
- New Tool - CSS Formatter and Optimizer
- Tools
- Automation Tip: How To Auto Set Copyright Years*
This post was created on October 22nd 2007 07:00 am



Comment by James Ehly
on 22 Oct 2007 at 9:42 am #
Well I just figured out a logical error in the script. If you find by state first then that might not actually be the closest location to you (since there might be a closer location in the state next to you). Just comment out the lines about querying the state first and you should be fine. However, I assume doing this will slow the code down quite a bit if you have a ton of locations to search against. Maybe caching the queries somehow would be in order.
James
Pingback by October 2007 Recap and Traffic Report | DEVTRENCH: Web Development from the Front Lines
on 02 Nov 2007 at 7:57 am #
[...] spam and web stats: Usi…How to Code a Simple Zip Code Loc…WPA Can also be Cracked!!…Out sick: Preparing for a regular…Getting Technorati Authority [...]
Comment by Internet Television
on 24 Jan 2008 at 4:16 pm #
That’s absolutely beautiful, great work.
Comment by josh
on 01 May 2008 at 8:17 am #
What do i have to do to show more stores?
Comment by James Ehly
on 01 May 2008 at 8:44 am #
A few people have asked me this, so here is the looping code you’ll need.
Find this line and add some code like this…
// now we can display closest store// optionally you could loop over this to list multiple stores
$store = $storeinfo[key($stores)];
print "<p>The web developers closest to: $findzip</p><br>\n";
$maploc = "’You are here’,'$findzip’,";
foreach($stores as $k=>$v) {
$output .= "<h3 style=’margin:0;padding:0′><b>".$info[$k]['company']."</b><br>(approx ".round($v)." miles)</h3>";
$output .= "<p style=’margin:0 0 10px 0;padding:0′><a href=’".$info[$k]['url']."’>".$info[$k]['url']."</a><br>";
$output .= $info[$k]['city']." ".$info[$k]['state'].", ".$info[$k]['zip']."</p>";
$maploc .= "’".$info[$k]['company']."’,'".$info[$k]['address1']." ".$info[$k]['city']." ".$info[$k]['state']." ".$info[$k]['zip']."’,";
}
$maploc = substr($maploc,0,-1);
And here are the changes to the javascript:
<script>// this is the address we want to map (you can list several and it will mark each one)
var addresses = new Array(<?=$maploc?>);
// Create and display Map object at the address with zoom level 3
// Include your application ID.
var map = new Map("mapContainer", "[your api key here]", "<?=$maploc?>",20);
map.addEventListener(Map.EVENT_INITIALIZE, onMapInit);
map.addEventListener(Map.EVENT_MARKER_GEOCODE_SUCCESS,onMarkerGeocode);
function onMapInit(eventObj) {
map.addTool( new PanTool(), true );
for(var i=0; i<addresses.length; i=i+2) {
//alert(addresses[i]);
//alert(addresses[i+1]);
// if you did do multiple addresses, then this would have to change some (because it would be the same store each iteration)
var marker = new CustomPOIMarker(addresses[i], ”, addresses[i+1], ‘0×000000′, ‘0xFFFFFF’);
map.addMarkerByAddress( marker, addresses[i+1] );
}
map.addWidget( new ZoomBarWidget() );
}
function onMarkerGeocode(eventObj) {
var geocodeResponse = eventObj.response;
}
</script>
Basically what you are doing is building an array of results and looping over then with the javascript.
Hope this helps. James