搜索城市 州 邮政编码

发布于 2024-11-15 02:22:08 字数 2114 浏览 6 评论 0原文

所以我已经有了每个城市、州、邮政编码的位置数据库。

我目前正在研究如何进行搜索并获取 x 英里之外的周围邮政编码的结果,但遇到了一些麻烦。

我成功地让您输入邮政编码进行搜索,因为它是预定义的值。但我对一个城市、州的结果遇到了麻烦。目前,它仅在您仅输入城市名称(例如“芝加哥”)时才有效...但如果您输入“芝加哥,伊利诺伊州”则不起作用。

这是搜索表单的 $_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 并获取周围的邮政编码。如果你输入邮政编码,它就会成功。如果您只输入城市名称,则可以成功运行。如果您输入“伊利诺伊州芝加哥”,则不起作用。

       <?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);
    ?> 

So i already have a database of locations of every city, state, zip.

I am currently working on how to do a search and get results of surrounding zip codes x miles away, and have run into some trouble.

I successfully have the search working for you to type in the zip code, since it is a predefined value. But am having trouble with results of a city, state. Currently it only works if you type in just the city name ex "Chicago"... But Does not work if you type in "Chicago, IL".

Here is the code for the $_GET of the search form, searchval

Please Help!

$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;
}

Below is the code that actually takes the $zip or $city and gets the surrounding zip codes. if you enter a zip code, it works successfully. If you enter just a city name, it works successfully. If you enter "Chicago, IL" it does not work.

       <?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);
    ?> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

颜漓半夏 2024-11-22 02:22:08

能够使用这个功能解决它......
返回 $arr[city] $arr[state] $arr[zip] 的数组

    function retcszarr($loc){
  $usstatenames=array('ALABAMA','ALASKA','AMERICAN SAMOA','ARIZONA','ARKANSAS','CALIFORNIA','COLORADO','CONNECTICUT','DELAWARE','DISTRICT OF COLUMBIA','FEDERATED STATES OF MICRONESIA','FLORIDA','GEORGIA','GUAM','HAWAII','IDAHO','ILLINOIS','INDIANA','IOWA','KANSAS','KENTUCKY','LOUISIANA','MAINE','MARSHALL ISLANDS','MARYLAND','MASSACHUSETTS','MICHIGAN','MINNESOTA','MISSISSIPPI','MISSOURI','MONTANA','NEBRASKA','NEVADA','NEW HAMPSHIRE','NEW JERSEY','NEW MEXICO','NEW YORK','NORTH CAROLINA','NORTH DAKOTA','NORTHERN MARIANA ISLANDS','OHIO','OKLAHOMA','OREGON','PALAU','PENNSYLVANIA','PUERTO RICO','RHODE ISLAND','SOUTH CAROLINA','SOUTH DAKOTA','TENNESSEE','TEXAS','UTAH','VERMONT','VIRGIN ISLANDS','VIRGINIA','WASHINGTON','WEST VIRGINIA','WISCONSIN','WYOMING');
  $usstateabbrs=array('AL','AK','AS','AZ','AR','CA','CO','CT','DE','DC','FM','FL','GA','GU','HI','ID','IL','IN','IA','KS','KY','LA','ME','MH','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','MP','OH','OK','OR','PW','PA','PR','RI','SC','SD','TN','TX','UT','VT','VI','VA','WA','WV','WI','WY');
    if(strpos($loc,',')!==false){
      $parts=array_map('trim',explode(',',$loc));
        $location['city']=strtoupper($parts[0]);
      preg_match('/([^ ]*)(?: +([^ ]+))?/',$parts[1],$statezip);
      if(isset($statezip[1])){
        $location['state']=strtoupper($statezip[1]);
      }
    if(isset($statezip[2])){
        $location['zip']=$statezip[2];
      }
    } else {
        $parts=array_map('trim',explode(' ',$loc));
      while(count($parts)>0){
            $part=strtoupper(array_pop($parts));
            if(in_array($part,$usstateabbrs)){
            $location['state']=$part;
          } elseif (in_array($part,$usstatenames)){
            $location['state']=$usstateabbrs[array_search($part,$usstatenames)];
          } elseif (preg_match('/\d+(?:-\d+)?/',$part,$zip)){
            $location['zip']=$zip[0];
          } else {
                $location['city']=strtoupper(implode(' ',$parts)."$part");
              break;
            }
      }
  }
    ksort($location);
    return $location;
}

Was able to solve it using this function...
returns an array of $arr[city] $arr[state] $arr[zip]

    function retcszarr($loc){
  $usstatenames=array('ALABAMA','ALASKA','AMERICAN SAMOA','ARIZONA','ARKANSAS','CALIFORNIA','COLORADO','CONNECTICUT','DELAWARE','DISTRICT OF COLUMBIA','FEDERATED STATES OF MICRONESIA','FLORIDA','GEORGIA','GUAM','HAWAII','IDAHO','ILLINOIS','INDIANA','IOWA','KANSAS','KENTUCKY','LOUISIANA','MAINE','MARSHALL ISLANDS','MARYLAND','MASSACHUSETTS','MICHIGAN','MINNESOTA','MISSISSIPPI','MISSOURI','MONTANA','NEBRASKA','NEVADA','NEW HAMPSHIRE','NEW JERSEY','NEW MEXICO','NEW YORK','NORTH CAROLINA','NORTH DAKOTA','NORTHERN MARIANA ISLANDS','OHIO','OKLAHOMA','OREGON','PALAU','PENNSYLVANIA','PUERTO RICO','RHODE ISLAND','SOUTH CAROLINA','SOUTH DAKOTA','TENNESSEE','TEXAS','UTAH','VERMONT','VIRGIN ISLANDS','VIRGINIA','WASHINGTON','WEST VIRGINIA','WISCONSIN','WYOMING');
  $usstateabbrs=array('AL','AK','AS','AZ','AR','CA','CO','CT','DE','DC','FM','FL','GA','GU','HI','ID','IL','IN','IA','KS','KY','LA','ME','MH','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','MP','OH','OK','OR','PW','PA','PR','RI','SC','SD','TN','TX','UT','VT','VI','VA','WA','WV','WI','WY');
    if(strpos($loc,',')!==false){
      $parts=array_map('trim',explode(',',$loc));
        $location['city']=strtoupper($parts[0]);
      preg_match('/([^ ]*)(?: +([^ ]+))?/',$parts[1],$statezip);
      if(isset($statezip[1])){
        $location['state']=strtoupper($statezip[1]);
      }
    if(isset($statezip[2])){
        $location['zip']=$statezip[2];
      }
    } else {
        $parts=array_map('trim',explode(' ',$loc));
      while(count($parts)>0){
            $part=strtoupper(array_pop($parts));
            if(in_array($part,$usstateabbrs)){
            $location['state']=$part;
          } elseif (in_array($part,$usstatenames)){
            $location['state']=$usstateabbrs[array_search($part,$usstatenames)];
          } elseif (preg_match('/\d+(?:-\d+)?/',$part,$zip)){
            $location['zip']=$zip[0];
          } else {
                $location['city']=strtoupper(implode(' ',$parts)."$part");
              break;
            }
      }
  }
    ksort($location);
    return $location;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文