Solved

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

Posted on 2012-03-10
3
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 22

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 83

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Node.js 11 73
How do I Check for duplicate entries in mysql 15 48
php hashing methods 3 45
How can I get these two for loops to talk to one another politely? 6 46
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

738 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