CakePHP Zip Code Helper

Bookmark and Share

If you need to calculate distance in a web app, or maybe say something like “User1 lives approximately 2.1 miles from User2″ then you’ve had to do the math to calculate distances between zip codes. It’s a great pain. In the past I’ve always used this really great class from MicahCarrick called PHP Zip Code Range and Distance Calculation class v1.2.0 ,for an upcoming CakePHP app that I’m building I need to do something like this. So I took Mr. Carrick’s class and converted it into a CakePHP Helper. Read on for code samples and instructions…



Please note that I did not create this class, I only created a wrapper for it. Full credit should go to Micah Carrick for creating it.



Step I. Preparing the Way…

Download the attached file. CakePHP ZipCode Helper v0.1 (Note that I have included Micah Carrick’s file here, edited for CakePHP)

Unpacking the archive you will see a SQL folder, lets start there. Here are instructions straight from Micah’s site:
There are 6 files in the /sql directory which contain SQL statements intended for a MySQL database (though they should work just fine for any SQL database). The zip code database consists of over 40,000 records which would be too large a file for many configurations of phpMyAdmin. Therefore, I have broken the data into records of 10,000 resulting in 5 files. In phpMyAdmin, you can import these 6 files 1 at a time using the ‘Import’ tab. You MUST import the ‘create_table.sql’ file first, then each of the data files.

create_table.sql
data_1.sql
data_2.sql
data_3.sql
data_4.sql
data_5.sql

Grab a cup of coffee, that import will take a couple minutes. Once that’s done, go back to the unpackaged file that you downloaded and grab app\models\zipcode.php and place it in your Cake application’s app\models folder. Do the same with app\controllers\zipcode_controller.php, app\views\helpers\zipcode.php, app\views\zipcode\index.thtml placing them each in their corresponding folders in your Cake Application.

*note that I have named the template .thtml, if your like me and are using the 1.2 release of Cake, you’ll want to rename it as .ctp as .thtml will be deprecated with the official release of 1.2.
Step II. Checking it out!

At this point you should be able to hit: http://localhost/zipcode and see something that looks like the following:

If you get an error, go back and make sure you’ll got the sql imported correctly, and the files copied to the right location.

Part III: How it works

Here’s the code for the above sample (note that this code is in the view template included in the download)

Distance between 2 zipcodes

To compute the distance between 2 zipcodes use the following bit of code in your view:

 PHP |  copy code |?
1
2
 
3
get_distance(28031,28202); ?>
4
 
5
//format: $zipcode->get_distance(zipcode 1 as int, zipcode 2 as int);
6
 
7
//outputs something like: 2.2 miles
8
 
9

All zipcodes within a radius

To grab all the zipcodes within a radius of a zipcode, use the following code in your view:

 PHP |  copy code |?
01
$zips = $zipcode->get_zips_in_range('28031', 10, _ZIPS_SORT_BY_DISTANCE_ASC, true);
02
 
03
//you can use _ZIPS_SORT_BY_DISTANCE_DESC to put the furthest away at the top of the list
04
 
05
//output it
06
foreach ($zips as $key => $value) {
07
echo "Zip code <strong>$key</strong> is <strong>$value</strong> miles away from <strong>28031</strong>.
08
";
09
}
10
 
11

Part IV: The End

That’s it. This bit of code could have alot of usage in a real estate application, or some type of social meetup site or anything else that you can dream up. In case you missed the links above, Here’s the link to the file:
CakePHP ZipCode Helper

Also remember to glance through the helper itself, especially if you live outside the US, as there are a few constants defined so that you could switch this out to use Kilometers instead of miles, etc…

That’s all there is to it, easy as pie cake!

8 Responses to “CakePHP Zip Code Helper”

  1. links for 2007-01-03 « Richard@Home Says:

    [...] WebDev Kungfu » Blog Archive » CakePHP Zip Code Helper Uses american Zip codes, but the technique could be applied to British postcodes (tags: cakephp zipcode distance calculator) Posted by Richard@Home Filed in [...]

  2. malikyte Says:

    Have you modified the SQL data files from Micah’s original script? I know there are free zip code databases out there, but they’re always a pain to find…I never think to bookmark them.

  3. justinjohnson Says:

    Malikyte: No I did not modify the SQL scripts at all. Feel free to use this as a free zip code database if you need to.

  4. malikyte Says:

    Thanks for that, though I was more curious if the zipcode database that Micah had provided was any newer in your SQL dump version. Zipcodes are constantly changing, and I *think* his database was from 2002. If I find anything, would you like a link?

  5. Blog Says:

    Thanks Justin just what i needed! I am using CakePHP for two weeks no so i hope i can get this running :)

    regards,
    Michelle

  6. Maria Says:

    This helper looks great and just what I need. However when I click on the link for the download, I’m brought to a page but no download opens. Could you possibly email me the .zip file?

    Thanks!

  7. james Says:

    hey broken download link!!!! please fix

  8. Josey Says:

    Great script… I think!
    Download link seems to be dead. Any chance of fixing that?

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>