0

I've been at it a few hours now and just can't seem to get what I'm looking for. Here is an example of what I'm up to:

$query1=mysql_query("Select * From table1 Where name='blah'");
while($queryvalue1=mysql_fetch_array($query1)) {
    $array1 = $queryvalue1['Column1']
    $Options="";
    $query2=mysql_query("Select * From table2 Where id In ($array1)");
    while($queryvalue2=mysql_fetch_array($query2)) {
        $var1 = $queryvalue2['Column2'];
        $var2 = $queryvalue2['Column3'];
        $Options.="<OPTION VALUE=\"$var1\">".$var2;
    }
}

If I echo $var1 and $var2 in the loop I get all the appropriate values, but when I wrap them in the html tag and echo options it only gives me the first value. I can use the $Options code fine if it takes place in the first array loop, it's just when I use it in the loop for the array of the array.

I'm at my wits end. Does anyone know why this problem is? Does anyone know of a way to fix it? Any help would be much appreciated!

2 Answers 2

1

Apart from the missing semi-colon on line #3, the problem is you're clearing the $Options string inside the outer loop.

Try something like this

$options = array();
$query1=mysql_query("Select * From table1 Where name='blah'");
while($queryvalue1=mysql_fetch_array($query1)) {
    $array1 = $queryvalue1['Column1'];
    $query2=mysql_query("Select * From table2 Where id In ($array1)");
    while($queryvalue2=mysql_fetch_array($query2)) {
        $var1 = $queryvalue2['Column2'];
        $var2 = $queryvalue2['Column3'];
        $options[] = sprintf('<option value="%s">%s</option>',
                             htmlspecialchars($var1),
                             htmlspecialchars($var2));
    }
}
$options = implode(PHP_EOL, $options);
Sign up to request clarification or add additional context in comments.

1 Comment

Took me a while to discover anyone replied, but boy am I glad! Thank you so much; the script works perfectly now! Just, wow, can't thank you enough -- I was COMPLETELY lost.
1

The problem is probably that you have this in the while loop:

$Options="";

This will reset the collected <option> fields and is why you only see the last entry from the second loop in the page output.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.