Wednesday, August 26, 2009

Zipcode Locator / Proximity search with PHP.

You will require a database to setup for zip proximity search. The database will contain zip codes, latitude and longitude information.

Here is the link you can download for free: http://sourceforge.net/projects/zips/

You need a class from phpclasses to apply zipcode / proximity search for your website:

http://www.phpclasses.org/browse/package/3156.html

Example code:

HTML FORM

<html>

<head>
<title>Zipcode Proximity Search</title>
</head>
<body>
<form action="zipsearch.php" method="post">
<input type="text" name="zip" value="" />
</form>
</body>
</html>

Code of zipsearch.php



<?php

if(!empty($_GET['zip']))
$zipcode=$_GET['zip'];
else
header('LOCATION:index.html');

$miles=’50’;
//initialization, pass in DB connection, from zip code, distance in miles.
$zip = new ZipCodesRange($appconf['dbconnection'],$zipcode,$miles);

//configuration
$zip->setTableName('zip_codes'); //optional, default is zips.
$zip->setZipColumn('zip'); //optional, default is zip.
$zip->setLonColumn('longitude'); //optional, default is lon.
$zip->setLatColumn('latitude'); //optional, default is lat.

//do the work
$zip->setZipCodesInRange(); //call to initialize zip array

//zip code output, other processing can be done from this array.
$zipArray = $zip->getZipCodesInRange();

// NOW WE WILL USE IT TO RETRIEVE THE RECORDS FROM THE SQL TABLE.
// FOR EXAMPLE TABLE 'cars_lot' CONTAINS ZIPCODE AND INFORMATIONAL FIELDS.

// Starts from writing where clause. We will use these zipcodes in the where clause to query.

$where_string='';
$array_count=count($zipArray);
$count=0;
foreach($zipArray as $key=>$value){

if($count!=$array_count)
$where_string.="echo zip_code='".$value."' OR "; zip_code is field in cars_lot
// it will display zip_code='908033' OR
if($count==$array_count)
$where_string.="echo zip_code='".$value."'"; zip_code is field in cars_lot
// it will display zip_code='908033'

$count++;

}
$sql="SELECT * FROM car_lot WHERE ".$where_string."";

// THIS RECORDSET WILL RETURN ZIP PROXIMITY SEARCH RESULTS.
return $result=mysql_query($sql) or die (mysql_error());

?>


No comments:

Post a Comment

All rights are reserved. Nobleatom.com
Software Development Services.
Contact me: khubabmazhar596@hotmail.com

Web Design Increase Page Rank Internet blogs DigNow.net web directory1Abc DirectorySeo friendly web directory