HTTP/1.1 // 38.107.191.87 // // 36215 // // CCBot/1.0 (+http://www.commoncrawl.org/bot.html)

Wordtracker Keyword Research

How to Code a Simple Zip Code Locator in PHP

Oct
22
2007

3. Set up database and tables

Make a database, or use an existing one you have. I used phpMyAdmin to make a database called ‘Locator’.
You’ll need to set up the table for your zipcode now. Since this table is so huge, I had to do this in steps. Create the table first:

CREATE TABLE `zip_codes` (
  `zip` varchar(5) NOT NULL default '',
  `state` char(2) NOT NULL default '',
  `latitude` varchar(10) NOT NULL default '',
  `longitude` varchar(10) NOT NULL default '',
  `city` varchar(50) default NULL,
  `full_state` varchar(50) default NULL,
  UNIQUE KEY `zip` (`zip`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Now import the sql downloaded in step 2. This is a giant table, so if you are working on a remote server and using phpMyAdmin or a similar tool, you might have to insert the records in sections (I could do 10,000 at a time).

Now we need something to locate. I created a store locator, and my ’stores’ table looks like this:

CREATE TABLE `stores` (
  `id` int(11) NOT NULL auto_increment,
  `company` varchar(50) NOT NULL default '',
  `address1` varchar(50) default NULL,
  `address2` varchar(50) default NULL,
  `city` varchar(25) default NULL,
  `state` char(2) default NULL,
  `zip` varchar(5) NOT NULL default '0',
  `phone1` varchar(10) default NULL,
  `phone2` varchar(10) default NULL,
  `email` varchar(50) default NULL,
  PRIMARY KEY  (`id`),
  KEY `zip` (`zip`)
);

Obviously, the important part in your store table is the zip, and you can add any other info you want.

Go to to step 4 »


Pages: 1 2 3 4 5

10 Comments »

Allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
MyAvatars 0.2

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

 

[...] 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 [...]

 
MyAvatars 0.2

Comment by Internet Television
on 24 Jan 2008 at 4:16 pm # Subscribed to comments via email

That’s absolutely beautiful, great work.

 
MyAvatars 0.2

Comment by josh
on 01 May 2008 at 8:17 am #

What do i have to do to show more stores?

MyAvatars 0.2

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], '0x000000', '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

 
 
MyAvatars 0.2

Comment by Shan
on 30 Mar 2009 at 10:17 am # Subscribed to comments via email

This is awesome… but the multiple part is not working for me…
after I insert the zip I get
The web developers closest to: 11501

‘You are here’,'11501′,’You are here’,'11501′,”,’ ‘,”,’ ‘,”,’
‘Array ( [id] => 2 [name] => test1 [address1] => 450 Hillside Ave [address2] => [city] => New Hyde Park [state] => New York [zip] => 11040 [phone1] => 5551212[website] => http://www.mmaxtra.compavailable => no => Yes [kutbha_hr] => [kutbha_min] => [email] => info@mmaxtra.com )

So I get a blank map and still get one location? Would you happen to know what am I doing wrong?

 
MyAvatars 0.2

Comment by Tim
on 19 Jun 2009 at 6:58 pm # Subscribed to comments via email

How do you paginate this? I’ve been trying every which way I can and every result is wrong. Once I remove my pagination the results are in the correct order again.

 
MyAvatars 0.2

Comment by Tim
on 23 Jun 2009 at 8:45 am # Subscribed to comments via email

Thanks for putting this out here!

What do you do for pagination? I got it working (without Yahoo integration, as I don’t need it) and implemented pagination. When I did that, the results were all messed up. I can’t for the life of me figure out why. Do you have an example?

 
MyAvatars 0.2

Comment by Blech
on 04 Mar 2010 at 7:49 pm #

I hate to rain on this as it is a nice start but I’ve fought with this code for a deadline I have and unfortunately haven’t been able to get it working after 3 hours of fiddling with different parts of this code… pretty frustrating.

MyAvatars 0.2

Comment by James Ehly
on 04 Mar 2010 at 10:30 pm #

You must have missed the note at the top:

This tutorial is now out of date since Yahoo has changed the map API. I’ll try to make a new tutorial using the new Yahoo AJAX API soon.

The old API no longer works. I still haven’t gotten around to making a new one, so the best resource now is going to be the Yahoo Maps API pages. There’s lots of examples there on how the use the new mapping api. Plus I found this great guide just the other day for google maps:

http://code.google.com/apis/maps/articles/phpsqlsearch.html