--- Marian Briones <[EMAIL PROTECTED]> wrote:

> I'm trying to wrap up a project and one of the features is to match
> zip codes so someone can find a service provider by their location.  I
> want to match it to 4 digits of their 5 digit zip code.  So if the
> service provider has 94536 listed, if the person searching enters
> 94539, is there a way to strip out that last digit the person is
> entering and match on LIKE $POST_(zip)% when the query runs?
> 
> Help!  Customer is mad at me...
> 
> Marian


I think you will run into problems with this method unless you have a huge
number of service providers who are distributed consistently across the US
(based on the zip code sample you provided).

If you want the first four digits of a string input, that can be easily
obtained:

$zip5 = "92115"; // or an input from a form
$zip4 = substr($zip5, 0, 4);

$q = "SELECT * FROM tablename WHERE zipcode LIKE '$zip4%'";

You could also do so with SQL:

$q  = "SELECT * FROM tablename";
$q .= " WHERE zipcode LIKE CONCAT(MID('$zip5',0,4),'%')";

I haven't tested this last one but it should work.

Were I faced with a similar application requirement, I would either make a
table with a list of zip codes which defines each service provider's territory
OR use the latitude and longitude of the zip code centers to determine the
closest provider "as the crow flies."  

This latter is a little tricky to optimize to ensure rapid results.  However,
it involves getting a table of zip codes with their location info.  Free tables
of zip codes and lat. lon. data can be found for the 1990 census from the US
government.  (I have such a table as a .sql file).  There are established
formulae for determining the distance between two lat. lon. coordinates.  

If you try to match the home zip code against each one in your table and
calculate the individual distances, you will run into difficulties since there
are thousands of zip codes and the floating-point calculation does take a
certain amount of time.  Instead, you need to employ a trick to look for the
zip codes which are in a given "rectangle" on the surface of the Earth using
the BETWEEN clause of SQL.  

For this, you need to figure out the longitudes which are + and - n miles from
your client's zip and the latitudes which are + and - n miles from the zip.

If you decide to go this latter route, contact me privately by e-mail.  I have
put some code together to get this done.  I think the top part of this message
covers the question you asked, however.

James
_____


James D. Keeline
http://www.Keeline.com  http://www.Keeline.com/articles
http://Stratemeyer.org  http://www.Keeline.com/TSCollection

http://www.ITeachPHP.com -- Free Computer Classes: Linux, PHP, etc.
Summer Semester Begins Jun 20 -- New Classes Start Every Few Weeks.


Community email addresses:
  Post message: [email protected]
  Subscribe:    [EMAIL PROTECTED]
  Unsubscribe:  [EMAIL PROTECTED]
  List owner:   [EMAIL PROTECTED]

Shortcut URL to this page:
  http://groups.yahoo.com/group/php-list 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-list/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply via email to