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 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
-
-
//format get_zips_in_range('zip as string',num of miles as int, sort constant, true);
-
//this returns an array that looks something like this (key is the zipcode and distance is value):
-
-
//$zips[28102] = 2.2;
-
-
//output it
-
foreach ($zips as $key => $value) {
-
echo "Zip code <strong>$key</strong> is <strong>$value</strong> miles away from <strong>28031</strong>.
-
";
-
}
Details about a zipcode
To view the details about a particular zipcode, use the following bit of code in your view
-
$details = $zipcode->get_zip_details('28031');
-
-
//format: get_zip_details('zip as string');
-
-
//returns an array with the bit of info as the key and the info value as the array value
-
-
//ex $details['city'] => 'Cornelius';
-
-
//output
-
foreach ($details as $key => $value) {
-
echo "$key: $value
-
";
-
}
-
?>
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!






January 3rd, 2007 at 12:19 am
[…] 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 […]
January 9th, 2007 at 11:04 pm
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.
January 10th, 2007 at 6:00 am
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.
January 10th, 2007 at 9:09 am
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?
December 6th, 2007 at 8:08 pm
Thanks Justin just what i needed! I am using CakePHP for two weeks no so i hope i can get this running
regards,
Michelle