I opened a thread a few days ago regarding running queries and loops within while loops in order to populate menus and show selected values. I was having a performance issue, but I realized later that the performance problem was caused by running the query within the while loop, and not just the fetch array. (duh!)

However, now that I'm running the fetch array and nesting a while loop within a while loop, I'm running into another problem. The nested fetch array seems to run fine, but I can't seem to carry the value of a variable that was set outside the nested while loop.

Here is a summary of the code:

$query1 = "select * from table1";
$result1 = mysql_query ($query1);

$query2 = "select * from table2";
$result2 = mysql_query ($query2);

##first while loop for data

while ($row1 = mysql_fetch_array($result1)) {

$id = $row1['id'];
$name = $row1['name'];

##second while loop to create drop down box data

while ($row2 = mysql_fetch_array($result2)) {

$id_option = $row2['id_option'];
$name_option = $row2['name_option '];

if ($id_option == $id){
$selected = "selected";
}
else
{
$selected = "";
}

$drop_down_box_options .= <option value=$id_option $selected>$name_option</option>";

}

}

The data returned with the above coding gives me the proper rows returned with the first while loop and I get the drop-downs populated for each row of data.

Where I'm having a problem is the line

if ($id_option == $id). The value $id does not seem to be carried from the first while loop into the second while loop.

I've never had a problem before with using a variable within a while loop that was established outside of a while loop.

I've done very little with nested while loops, so I'm not sure what is going wrong.

    You need to reset your pick list query each time you execute your inner loop.

    mysql_data_seek ($result2, 0);
    while ($row2 = mysql_fetch_array($result2)) {
    }
    

    It would actually be better to just turn $result2 into an index array and build your pick list from it.

      8 days later

      I tried this and I got each value returned twice. In other words, the drop down menu had each value listed twice (a,b,c,a,b,c). The $selected value, however, only got returned once. So why did I get this extra set of options returned? I tried specifcially restricting the while loop to the number of iterations of results rows, and this didn't solve it. So the while loop is being run two separate times, even though all the other results in the outer while loop are only being returned once for each iteration.

      I'm sure this is probably something about running nested while loops, but how do I correct this?

        Never mind.

        What was happening was that the display result ($drop_down_box_options) was being appended after each iteration of the outer while loop. So each successive row's drop box had an additional result set.

        So this was easily fixed by using a different variable name for each iteration of the outer loop

          Write a Reply...