用户附近的特定邮政编码:PHP mysql

时间:2014-01-01 15:15:15

标签: php mysql arrays search zip

新年快乐!假设user1位于邮政编码12345.我想在该邮政编码的X英里内找到其他用户。

首先,我创建了表单:

<div id="wrapper">
    <form action="L1.php" method="post">

        <select name="radius">
            <option value="5">5</option>
            <option value="10">10</option>
            <option value="20">20</option>
            <option value="30">30</option>
            <option value="40">40</option>
            <option value="50">50</option>
        </select>
        Miles within Zip Code:


        <input name="zip"  type="text" value="13126" />
        <input type="submit" value="refine" />
    </form>
</div>

现在我列出了距离12345 X英里内的所有邮政编码:(L1.php):

     <?php
        include('includes/db_AF.php'); //includes the db credentials
          $connection = @new mysqli(HOSTNAME, MYSQLUSER, MYSQLPASS, MYSQLDB);

            if (mysqli_connect_errno()) {
              printf("Connect failed: %s\n", mysqli_connect_error());
              exit();
            }
            //look up the zip code in the searchbox
          $whereClauses = array(); 
          if (! empty($_POST['zip'])) $whereClauses[] ="zip_code='".mysqli_real_escape_string($connection,$_POST['zip'])."'";
          $where = ''; 
          if (count($whereClauses) > 0) { $where = ' WHERE '.implode(' AND ',$whereClauses); }

          $sql = "SELECT * FROM zip " .$where." "; 

          $result=mysqli_query($connection,$sql) or die("Error: ".mysqli_error()."<br />Query: ".$sql);
        //find out the corresponding lat and long

            while ($row = mysqli_fetch_assoc($result)) {
              echo '<br />';

              echo '<br />';

              $lat=$row['latitude'];
              echo '<br />';

              $lon=$row['longitude'];
              echo '<br />';
            } 


                $radius=$_POST['radius'];

//here I am generating an array of all the zip codes within x miles from 12345.

            $query="SELECT * FROM zip  WHERE (3958*3.1415926*sqrt((latitude-'$lat')*(latitude-'$lat') + cos(latitude/57.29578)*cos('$lat'/57.29578)*(longitude-'$lon')*(longitude-'$lon'))/180) <= '$radius'";


            $result_obj = '';
            $result_obj = $connection->query($query);           


            while($resultx = $result_obj->fetch_array(MYSQLI_ASSOC)) {  
            $items[] = $resultx;
            }               

            foreach ($items as $item) {
            echo    $item['zip_code'];
                echo '<br />';
            }

    $queryz="SELECT zip FROM customer"; // I am generating an array of all customer zip codes.              


            $resultz_obj = '';
            $resultz_obj = $connection->query($queryz);         


            while($resultzz = $resultz_obj->fetch_array(MYSQLI_ASSOC)) {    
            $itemsz[] = $resultz;
            }

        $resultv = array_intersect($items, $itemsz);
        print_r($resultv);

所以现在我有一系列邮政编码。我想列出那些邮政编码的用户。我知道我必须交叉两个数组,但得到“注意:数组到字符串转换”错误。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果您在$items中有邮政编码,那么我认为您对所有客户邮政编码都不需要$itemsz

首先,您需要将$ items数组格式化为:

$ zips =(12345,12346,12347,12348)

然后在客户中运行这样的查询:

Select * from customer where customer.zipcode IN $zips

演示: https://eval.in/84652
    示例代码:

$items =array( '0' => array( 'zip_code' => 13121, 'latitude' => 43.483379, 'longitude' => -76.315044, 'city' => 'New Haven', 'state' => 'NY', 'county' => 'Oswego' ), '1' => array( 'zip_code' => 13126, 'latitude' => 43.465388 ,'longitude' => -76.342172 ,'city' => 'Oswego' ,'state' => 'NY', 'county' => 'Oswego' ) );

$strzipCodes="";
foreach($items as $item){

    $strzipCodes .= $item['zip_code']." ,";

}
$strzipCodes = rtrim($strzipCodes,',');

$sql ="SELECT * FROM customer where zip IN(".$strzipCodes.")";
echo $sql;
// then execute query we will get all users inside those zipcodes

我想你明白了。