0
因此,我已經有了每個城市,州,郵編的位置數據庫。搜索城市狀態郵編
我目前正在研究如何做一個搜索,並得到周圍的郵政編碼x英里遠的結果,並遇到了一些麻煩。
我成功讓您的搜索功能可以輸入郵政編碼,因爲它是一個預定義的值。但是,對於一個城市,州的結果有困難。目前只有輸入城市名稱「芝加哥」時纔有效......但如果輸入「Chicago,IL」,則不起作用。
這裏是_GET的$搜索表單的代碼,searchval
請幫助!
$searchval = $mysql->escape_data($_GET['searchval']);
if (!empty($searchval))
{
if(preg_match("~\d{5}~", $searchval)) {
$zip = $searchval;
}
else {
$city = $searchval;
}
} else
{
$error .= "<div id='Error'>Please enter zip</div>";
$zip = false;
}
以下是實際使用$ zip或$ city並獲取周圍郵政編碼的代碼。如果你輸入一個郵政編碼,它會成功。如果只輸入一個城市名稱,則可以成功運行。如果你輸入「芝加哥,IL」它不起作用。
<?php
//if user entered a city ex. "Chicago"
if(isset($city)) {
$q = 'SELECT City, State, ZipCode FROM zipcodes WHERE City like "'. $city .'%" ORDER BY ZipCode ASC Limit 0,10';
$result = $mysql->query($q);
$row = mysqli_fetch_array($result);
$zip = $row[2];
if(mysqli_num_rows($result) != 1) {
echo"Did you mean...<br />";
while($row = mysqli_fetch_array($result)) {
echo "<a href='" .$_SERVER['PHP_SELF'] . "?searchval=".$row[2]."&miles=".$miles."'>" . $row[0] . " " . $row[1] . " " . $row[2] . "</a><br />";
}
}
}
$zcr = new ZipCodesRange($mysql,$zip,$miles);
$zcr->setNewOrigin($zip);
//if user entered a zip code ex. "08026"
if($zcr->validateZipCode($zip)) {
$citystate=$zcr->getCityState($zip);
echo "Zip Codes Within " . $miles ." miles of " . $citystate[0] . ", " . $citystate[1] . " " . $zip;
}
$zcr->setZipCodesInRange();
$zipArray = $zcr->getZipCodesInRange();
asort($zipArray);
$z = implode(",", array_keys($zipArray));
$q = "SELECT * FROM " . TBL_PUBS . " WHERE zip IN ($z) AND(status = 'Pending' OR status = 'Active')";
$result = $mysql->query($q);
while ($row = $result->fetch_object()) {
$lonlat=$zcr->getLonLat($row->zip);
$distance = $zcr->calculateDistance($lonlat[1], $lonlat[0], $zip);
?>