HI!

I am trying to find a way to plug some data that is spit out of a mysql query into another mysql query.

example.

$zip1 = $_POST[zip1];

"SELECT * FROM zipcodedb WHERE ZipCode >= '". $zip1 ."'"

lets say i get that query to spit out these 3 results with a while loop :
11758
11759
11760

now i need to take those three zipcodes and plug them into another mysql query

"SELECT * FROM zipcodedb WHERE ZipCode = 11758 OR ZipCode = 11759 OR ZipCode = 11760"

The part i am stuck on is this.
I need to figure out a way to dynamically populate the WHERE portion of this query because the above zipcodes will change depending on what was entered and submitted into the FIRST mysql query.

I'm trying to explain best as i can, my english is not perfect.
Thank you for any help anyone can offer.

Recommended Answers

All 4 Replies

this should work

$sql = "SELECT * FROM zipcodedb WHERE ";
$count = count($sqlarray);
$x =1;

foreach ($sqlarray as $zipcode)
{
    $sql .= "ZipCode = ".$zipcode;
    if($x++ < $count)
    {
        $sql .= " or ";
    }
}

Thank you for your response Kireol!

This looks like it is exactly what i need, but unfortunatly i need some further clarification on your code.

could you perhaps explain to me what i need to do with the $sqlarray and $zipcode variables before your code actually runs?

i'm assuming i need to set the results from the first sql query which spits out the 3 different zipcode in my original example into an array of some sort? i have never dealt with arrays before, so alittle extra help with this would be GREATLY appreciated. I understand and pick up on things quickly, so just a basic rundown of the process should be sufficient enough for me to get the idea and run with it.

Thank you again!

$sqlarray should be an array of zip codes. the result of your previous query. For testing and playing pursposes, you could manually make an array like:

$sqlarray = array ('12345','45678','76543');

$zipcode gets filled by PHP. It pops the first element off of the array specified in the foreach and each loop iteration it will fill the current value.

So the first time through the loop zipcode will = '12345' and the 2nd time '45678' and so on, given the sample array I posted above.


To take foreach and arrays 1 step further, you can also get the key

foreach($sqlarray as $key=>$value)

if you have a regular array, $key will have the array number (you COULD use this to replace my $x variable if you wanted to.) If it's an associative array 'customer1'=>'12345' then $key will have 'customer1' and value will have '12345'

hope that helps

EXCELLENT!

Thank you so much!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.