-1

I am passing several zip codes(retrieved by a previous lat/long search) and other data via GET to another page that will retrieve all listings from a database that contains those zip codes in the address.

So the url looks like this:

http://blahblahblah.com/listings.php?date=2015-10-23&zc=0=23547&1=28456&2=27678

My question is how do I loop through those zip codes in my db query? It seem like running a separate query for each zip code would be pretty taxing if given to many zipcodes.

$zc = array(); //initiate the array
$zc[0] = $_POST['zip']; // Add the starting zip code to the array

mysqli_query($conn, "select * zip_code within a certain range of lat and lng");
// return results
while($row = mysqli_fetch_assoc($sql)){
  $zip = $row['db_zip'];
  array_push($zip);
}

So there is how I am getting the info into the array. After that it is on to building the URL to display a link on a calendar for each day of the month that there is a listing.

//A ton of other stuff then 
//echo the link using html_build_query() to add the zips to the get parameters.
echo '<a href="listing.php?date='.$l_date.'&zc='.http_build_query($zc).'">'.$d_count.'</a>';

So then I am on to processing the info on the next page, which is where I am really confused.

user3154948
  • 137
  • 2
  • 11
  • 1
    `SELECT * FROM table WHERE zip_code IN (23547, 28456, 27678);` – Mike Oct 23 '15 at 19:27
  • 1
    You don't. You would need this URL: `listings.php?date=2015-10-23&zc[]=23547&zc[]=28456&zc[]=27678` – AbraCadaver Oct 23 '15 at 19:27
  • @Mike OK Mike, I like it, that would surely make it easy but what would I use to break the array up or do I just can I just use something like SELECT * FROM table WHERE zip_code IN ($zc[]) ? Sorry arrays and I really don't mix, I just can't get my head around them and how to parse them for some reason. – user3154948 Oct 23 '15 at 19:34
  • @AbraCadaver I am using http_build_query() I tried to build the url as you specified but it threw and error so I went with http_build_query() instead and this is the URL it pops out. – user3154948 Oct 23 '15 at 19:37
  • @user3154948 I'm not sure which database API you're using, but assuming PDO, here's how you can bind an unknown number of parameters to the query: http://stackoverflow.com/questions/6071619/pdo-bind-unknown-number-of-parameters – Mike Oct 23 '15 at 19:38
  • Need to use an array of zc: http://sandbox.onlinephpfunctions.com/code/62f6fd1487c2b25ebf6cb2e1e211d8ecf28eb614 – AbraCadaver Oct 23 '15 at 19:43
  • ok wait, can we back up a second... I am going to update my original post with how I am getting the info and how I am getting into the array in the first place, because at this point I am confused. – user3154948 Oct 23 '15 at 23:07

2 Answers2

-1

Ok guys given the number of zip codes I could end up passing, GET was not going to work.

The solution that Mike gave was indeed correct, however, it had to be done by passing the zip codes to the next page via sessions.

Like so:

$_SESSION['zc'] = $zc;

and then imploding the array using the imploded data in the query:

        $tmp = implode(", ", $_SESSION['zc']);

        $sql = mysqli_query($conn, "SELECT * FROM listingss WHERE zipcode IN ($tmp) AND l_date = '$date'");
user3154948
  • 137
  • 2
  • 11
-2

listings.php?date=2015-10-23&zc[]=23547&zc[]=28456&zc[]=27678

use implode function of php like this $tmp = implode(",", $zc);

and then

SELECT * FROM TABLE WHERE zip_code in ($tmp);

hpatel
  • 1
  • 1