Solved

How can I shorten a zip code to its first 3 digits?

Posted on 2012-03-10
3
357 Views
Last Modified: 2012-06-27
Hi guys,
I'm allowing a user to search my  db by using a zip code to find merchants in that zip code.
But I also want to show results of zip codes nearby based on the 5 digit zip code entered.
Shortening the zip code to 4 digits will give me the results I want. I'll also want to expand that search to a much broader area using the same input by shortening that input to 3 digits.

So what do I write after the following:
$5_zip=$_REQUEST['5_zip'];

Thanks for the help,
Sas
0
Comment
Question by:sasnaktiv
3 Comments
 
LVL 21

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 37705795
What kind of database do you have? This is more of a database function than a php function. You have to request all the businesses whose zip codes begin with the first three digits of the requested zip code.

Getting the first three digits is very basic. Use the substr (substring) function. (By the way, php variable names must start with a letter or underscore. See PHP: Basics - Manual, second paragraph.)

$zip3 = substr($_REQUEST['5_zip'],0,3);

Open in new window

0
 
LVL 1

Author Closing Comment

by:sasnaktiv
ID: 37705808
Thank you very much. That's exactly what I needed. Have a good weekend.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 37705813
You should know that Zipcodes are not assigned exactly by proximity.  For example, in the area Northeast of Jackson in Northern California, you can find the following adjacent Zipcodes:

95685 - 95665 - 95666 - 95642 - 95255 - 95232
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now