简体   繁体   中英

users near a specific zip code: PHP mysql

Happy new year! Suppose user1 is located at zip code 12345. I want to find other users within X miles from that zip code.

First, I created the form:

<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>

Now I am listing all the zip codes within X miles from 12345: (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);

So now I have an array of zip codes. I want to list users that live at those zip codes. I know I have to intersect two arrays but get "Notice: Array to string conversion" error. Any thoughts?

If You have zip code in $items then i think you don't need $itemsz for all customer zipcodes.

First you need to format $items array to like this :

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

Then just run a query like this in customer:

Select * from customer where customer.zipcode IN $zips

demo: https://eval.in/84652
SAMPLE code:

$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

I think you get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM