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.