Forums

This topic is locked

creating a search by postcode query

Posted 16 Mar 2005 16:52:07
1
has voted
16 Mar 2005 16:52:07 harpal padwal posted:
hello!

I need to create a search faciltity that will display all postcode records within either 10, 15, 20, 30, 50+ miles from an entered postcode ny the user.

similar to the search in www.autotrader.co.uk.

Im using ASP with an access database.

How can this be done?

Replies

Replied 06 Jan 2006 00:18:09
06 Jan 2006 00:18:09 Al Patel replied:
Andrew I hope you're doing alright. Your test page for the zip code search was very nicely done. Are you able to share the code?

Thanks, al
Replied 08 Jun 2007 01:38:08
08 Jun 2007 01:38:08 Bobby Edgar replied:
Hello Everyone. I'm here to help with the old question, "How Do I Create A Form To Return "X" Zipcodes From A Distance of "Y" From a "Z" Zipcode!!!

Verify That This Is What You Want:

stagepros.com/zipfun.html

It's written in PHP and I don't use ASP, so sorry to all who don't use PHP.

If So...Here's Your Code.

First Things Firt...Create The DB Structure (MySql)

CREATE TABLE zipData (
zipcode varchar(10) NOT NULL,
lon varchar(20) DEFAULT ‘0.00000’ NOT NULL,
lat varchar(20) DEFAULT ‘0.00000’ NOT NULL,
city varchar(100) DEFAULT ‘’ NOT NULL ,
state varchar(5) DEFAULT ‘’ NOT NULL ,
PRIMARY KEY (zipcode)
);


Now Create The Search Form Page Using This Form And Save "zipfun.html"
** Remove The Quotes From The "<" From The Code Below. **
"<"form name="form1" method="post" action="zipFun.php">
Find Zipcodes within
"<"label>
"<"input name="radius" type="text" id="radius" size="4">
"<" /label>
miles from Zipcode
"<"input name="zipcode" type="text" size="5">
"<"input type="submit" name="Submit" value="Submit">


Now create the PHP page to make it all happen. ZipFun.php
<pre id=code><font face=courier size=2 id=code>
&lt;?php
$dbUsername = "YOUR MYSQL LOGIN NAME";
$dbPassword = "YOUR PASSWORD";
$dbHostname = "YOUR MYSQL HOSTNAME";
$dbDatabase = "YOUR MYSQL DB";

$db = mysql_connect($dbHostname, $dbUsername,$dbPassword) or die("Could not connect";
mysql_select_db($dbDatabase) or die("Could not select database";

function inradius($zip,$radius)
{
$query="SELECT * FROM zipdata WHERE zipcode='$zip'";
$result = mysql_query($query);

if(mysql_num_rows($result) &gt; 0) {
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$lat=$row["lat"];
$lon=$row["lon"];
$query="SELECT * FROM zipdata WHERE (POW((69.1*(lon-\"$lon\"*cos($lat/57.3)),\"2\"+POW((69.1*(lat-\"$lat\"),\"2\")&lt;($radius*$radius) ";
$result = mysql_query($query);
if(mysql_num_rows($result) &gt; 0) {
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$zipArray[]=$row;
}
return $zipArray;
}
} else {
return "Zip Code not found";
}
} // end func

$zipCode = $HTTP_POST_VARS["zipcode"];
$radius = $HTTP_POST_VARS["radius"];

$zipArray = inRadius($zipCode,$radius);

print "There are ".count($zipArray)." Zip codes within $radius Miles of $zipCode";
foreach($zipArray as $row) {
print "&lt;br&gt;ZipCode:$row[zipcode] City: $row[city]";

}
?&gt;
</font id=code></pre id=code>
You can get the Zipcode DB at ZipCodeWorld.com. Just rename the fields to match the ones in the SQL above. You'll change Zip_Code to zipcode, LATITUDE to lat, LONGITUDE to lon, CITY to city, and STATE to state. DON'T CHANGE ANY OF THE FIELD TYPES IF YOU USE THE ZipCodeWorld.com DB! I also suggest making a copy of the DB for backup. Make a copy and call it "zipdata"

I hope this helps you all. ENJOY!!!

Reply to this topic