...

View Full Version : how to have form to give results to user based on zip code



msguided
02-24-2010, 02:39 PM
I want to have a small form on my homepage that has a space for a user's email address and city, state, zip code. Based on the input, I want the form to generate an automatic response to the user. For example, if my site deals with dentists, I want the form to automatically send her the names and contact info of 4 dentists in that area (provided they are dentists listed on my site). How would I go about doing this? Any info would be greatly appreciated. Thanks!

Fumigator
02-24-2010, 03:23 PM
You want to do an exact match on zip code? Or you want to find zip codes geographically close to the zip code entere on the form? The two requirements will be vastly different in complexity.

angst
02-24-2010, 04:03 PM
I've done this before, you will need to do a few things here;

first you will need to get the lat/long or the addresses in question, you can do this using google maps api, this is the function that I build:



Function GetLatLng($Address,$Type){
// request URL
$url = "http://maps.google.com/maps/geo?q=" . urlencode($Address) . "&output=xml&key=" . GOOGLE_MAP_KEY;

// get file
$ch = curl_init($url);
$Temp = LOCAL_TEMP_PATH . "GoogleLocation.xml";
$fp = fopen($Temp, "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // makes our request look like it was made by Firefox
curl_exec($ch);
curl_close($ch);
fclose($fp);

// read file
$page = @file_get_contents($Temp);
$xml = new SimpleXMLElement($page);
$Result = $xml->Response->Placemark->Point->coordinates;
$LatLng = explode(",",$Result);
if($Type == "Lng") return $LatLng[0];
if($Type == "Lat") return $LatLng[1];
}


send request:


function SendRequest($url){
//echo $url; exit;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // forces response into return string, not echo
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") );
return curl_exec($ch);
curl_close($ch);
}




you should store this data in your database, same table as the dentists,

then you need to get the coordinates of the client ( person doing the lookup )



function getCoord(){
$d = $this->SendRequest("http://maps.google.com/maps/geo?q=" . urlencode($this->PostalCode) . "&output=xml&key=" . GOOGLE_MAP_KEY);
if (!$d)
return false; // Failed to open connection

$coord = new SimpleXMLElement($d);

if ((string) $coord->Response->Status->code != '200')
return false; // Invalid status code

list($lng, $lat) = explode(',', (string) $coord->Response->Placemark->Point->coordinates);
return array('Lat' => (float) $lat, 'Lng' => (float) $lng);
}


then, you need to calculate the distance, and display the list;



$dst1 = $StoreLocator->getCoord(); // for user lng/lat
$Distance = "";
// get store locations lng/lat
$result = mysql_query("SELECT City,LocationsID,ProvinceName,City,MapLat,MapLng FROM Locations l, Provinces p WHERE l.LocationsID !=34 AND l.ProvincesID = p.ProvincesID ORDER BY LocationsID ASC");
While($row = mysql_fetch_array($result)){
$kms = rad2deg(acos(sin(deg2rad($dst1['Lat'])) * sin(deg2rad($row['MapLat'])) +
cos(deg2rad($dst1['Lat'])) * cos(deg2rad($row['MapLat'])) *
cos(deg2rad($dst1['Lng'] - $row['MapLng'])))) * 60 * 1.1515 * 1.609344;
$Distance[] = array(round($kms,1),$row['LocationsID'],$row['City'],$row['ProvinceName']);
}

$Counter = 0;
$DistanceArray = $StoreLocator->msort($Distance,0);

foreach ($DistanceArray as $value){
$Counter++;
echo "<div class='LocationCityRow' onClick=\"location.href='/location/" . $value[1] . "/';\">" . $value[3] . " &bull; " . $value[2] . " &bull; Distance: " . $value[0] . "Kms</div>\n";
if($Counter == 5) break;
}



ofcourse these functions are built for my site, but you can mod them to work for you.
hope this helps.

msguided
02-24-2010, 06:35 PM
Thanks for the help, but I think this is probably way over my head. :(



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum