Q:

Trying to make dynamic option form

OK I try this for days and I can't do it so I try to take it step by step first I need to get the info from the database correctly and show it in a form here is the database
CategoryID  |  CategoryName  |    ParentID

----------------------------------------
1           | FirstMenuCat1  |      0
2           | FirstMenuCat2  |      0
3           | SubMenuCat1    |      1
4           | SubMenuCat2    |      1
5           | SubMenuCat3    |      2

and here is my last attemp that worked so I know I get the correct data from database but I don't know how to make it a form:
<?php
require_once ('mysqli_connect.php');
$q = "SELECT CategoryName FROM menus where ParentID = '0' ORDER by CategoryID";
$r = @mysqli_query ($dbc, $q) or die("Error: ".mysqli_error($dbc));

/* associative array */
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){
printf ("%s\n", $row["CategoryName"]);

}
 ?>

Here is my attempt to make it but its not working 
    

/* associative array */
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)){

foreach($row){
                echo "<option value=\"". urlencode( $_ ) ."\" $selected >$_</option>\n";
                $selected = ""; // only the first element will be marked as selected
}

}
 ?>

Any ideas? 

A:

By looking at option tag i think you are trying to create a combobox list. If it is then  First of all do not use \n. The options in a combobox list already shown line by line. 
Secondly you have not printed the select tag. Without it the list will not be rendered properly. Try the following code:
echo "<select name='mylist'>";

$selected = "selected";
while (($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) !== FALSE){
   echo "<option value='". $row['key']."' $selected >".$row['value']."</option>";
   $selected = ""; // only the first element will be marked as selected
}

echo "</select>";

hopefully it may help.

