I'm a new PHP user and could use some help creating a nested "While" loop. The code below creates a drop-down menu. For each value in the drop-down menu, I would like to create another drop-down menu. For instance, in the primary drop-down, you'd see Category1, Category2....CategoryN (with values pulled from the first field in database "test"). Then, if the user selects Category1, another drop-down appears (to the right) and provides SubCategory1, SubCategory2....SubCategoryN (pulled from the second field in database "test").
Questions:
1) Can I basically reuse the same "While" loop or do I have to make some significant modifications?
2) Can I use $result and $row in the second "While" loop?
3) If the answer to #1 above is yes, where do I start the second "While" loop? Immediately after the first "{"?
<select name="result">
<option value="0"
<?php
if (empty ($result)) echo "selected";
?>>- select -</option>
<?php
// set database server access variables:
// open connection
// select database
// create query
$query = "SELECT * FROM test";
// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
// While Loop
while($row = mysql_fetch_row($result)) {
echo " <option";
if ($result==$row) { echo " selected"; }
echo ">", $row[0],"</option>\n";
}
// free result set memory
mysql_free_result($result);
// close connection
mysql_close($connection);
?>
</select>