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!
24 Responses
[...] 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 [...]
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.
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.
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?
Thanks Justin just what i needed! I am using CakePHP for two weeks no so i hope i can get this running
regards,
Michelle
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!
hey broken download link!!!! please fix
Great script… I think!
Download link seems to be dead. Any chance of fixing that?
I couldn’t get the download link to work, but I dug it up on the Internet Archive:
http://web.archive.org/web/20070321031039/https://webdevkungfu.com/cakephp-zip-code-helper/
Like Tom, I can’t get the download link to work. Any suggestions?
Simple article! Even for non-programmer!
Thanks for sharing!
It is a UK Distance Calculator and Driving Directions to calculate distance between cities, towns or villages in UK and Mileage Calculator. Distance Links use these to find what you need. Distance Calculator is a Measure the Distance from one place/town to another.
postcode distance calculator
our weblog brings me a great deal of enjoyable. Extremely glad to have the pportunity to meet you. Take ralax and give yourself a surprise, and we will live additional happy.
I’m brought to a page but no download opens. Could you possibly email me the .zip file?
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.
Simple article! Even for non-programmer!
Thanks for sharing!
This post is really helpful.As a fresher i will really keep this points in mind and have the passion to outperform my peers and contribute to my company from day one!!
Have the petals pulled out and have them scattered on the center of the table
fhdjhfsjd
If you buy bags and purses of the latest new designs from the louis vuitton outlet now, you can enjoy special discounts. What else are you waiting for?
Designs of this season give you the unlimited cool feelings in this cool summer. The new designs of Coach products in the coach outlet enrich our daily life.coach outlet online develops very quickly because it runs by its own special way and it contents the customers greatly.
If you have been to the mall lately and browsed Coach Outlet you may find that a high price tag goes along with just about everything in the mall.As long as you open our Coach Factory Outlet webpage, you will view various Coach New Arrivals which are the most popular also the most fashionable in this year.
This brown handbag called Flagship Signature Brown Handbag has been the best seller in the Coach Factory Outlet for almost three weeks.
Pretty good post. I have only run your blog and I was meaning that I have appreciated very much your reading way posts. Any blog Ill to be when the feed was subscribing and I hope to post again soon.
If you have been to the mall lately and browsed Coach Outlet you may find that a high price tag goes along with just about everything in the mall.As long as you open our Coach Factory Outlet webpage, you will view various Coach New Arrivals which are the most popular also the most fashionable in this year.