Dynamic Making Select Dropdown Menu PHP MYSQL

Items From Database

This tutorial shows the backbone for which you can create a dynamic dropdown menu from any database column. You will pull only the distinct values for an entry and autoselect the option for which the user has chosen. This is a procedure for updating entries.

function dynamic_update_menu($my_id) {

$command= "SELECT DISTINCT size from table";
$result = mysqli_query($db, $command);
if ($result && mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
/*creates an array from the distinct names in the column. The process actually makes 3 separate arrays. You can see with print_r() function. The arrays are Array ( [0] => Small ) Array ( [0] => Small [1] => Medium ) Array ( [0] => Small [1] => Medium [2] => Large ) */
$sizes[] = $row['size'];
print_r($sizes);
}
}
echo '<select name="sizes">';

$command2= "SELECT size from table where id ='$my_id';";
$result2 = mysqli_query($db, $command2);
if ($result2 && mysqli_num_rows($result2) > 0) {
while ($row = mysqli_fetch_assoc($result2)) {

$myname=$row['size'];

foreach ($sizes as $key => $value) {

if($myname==$value) {
$selected="selected='selected'";
}
else {
//make other months not selected
$selected="";
}
//output the current month as selected
echo "<option value=\"$value\" $selected>$value</option>\n";
}
}
echo '</select>';
}
}

Items From Hard-Coded Array

function dynamic_update_menu($my_id) {

$sizes = array ('Small', 'Medium', 'Large');

echo '<select name="sizes">';

$command2= "SELECT size from table where id ='$my_id';";
$result2 = mysqli_query($db, $command2);
if ($result2 && mysqli_num_rows($result2) > 0) {
while ($row = mysqli_fetch_assoc($result2)) {

$myname=$row['size'];

foreach ($sizes as $key => $value) {

if($myname==$value) {
$selected="selected='selected'";
}
else {
//make other months not selected
$selected="";
}
//output the current month as selected
echo "<option value=\"$value\" $selected>$value</option>\n";
}
}
echo '</select>';
}
}