开发者

populating form option list with an array of an array

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开发者_如何学JAVA, 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!


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜