CakePHP Zip Code Helper

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

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:


get_distance(28031,28202); ?>

//format: $zipcode->get_distance(zipcode 1 as int, zipcode 2 as int);

//outputs something like: 2.2 miles

All zipcodes within a radius

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

$zips = $zipcode->get_zips_in_range('28031', 10, _ZIPS_SORT_BY_DISTANCE_ASC, true);

//you can use _ZIPS_SORT_BY_DISTANCE_DESC to put the furthest away at the top of the list

//output it
foreach ($zips as $key => $value) {
echo "Zip code $key is $value miles away from 28031.
";
}

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!