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

标签 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);

现在我有了一个邮政编码数组。我想列出居住在这些邮政编码的用户。我知道我必须使两个数组相交,但出现“注意:数组到字符串转换”错误。有什么想法吗?

最佳答案

如果您在 $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

我想你明白了。

关于php - 特定邮政编码 : PHP mysql 附近的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20870030/

相关文章:

php - 如何使用SLIM框架创建MySQL事务

mysql - 内存数据库和磁盘内存数据库的区别

C++ 用自定义键和值或类似的东西制作一个数组

javascript - 如何将对象迭代到平面列表中 - react native ?

php - MySQL 查询问题(发送到服务器的查询数量。)?

javascript - 通过 Ajax 在 Laravel 上动态依赖选择框,但通过不同的表获取数据,如何?

php - 代码点火器 : insert data in database

mysql - 比较 MySQL 查询中字符串的值

mysql - Node js - mysql任务完成后是否需要关闭连接?

C++ 指向数组返回类型的指针