I have 2 while loops in my php code. The first one prints out the ingredients of a meal in a button / table format for display. The 2nd while loop puts the ingredients into a list for selecting. However, when I run my code, only the first while loop runs successfully and the 2nd one does not.
I tried reversing the order, where the first while loop inputs the list and the 2nd prints out the table to the screen. I got the options menu filled up, while the table did not print out, so I know that I didn't mess up in writing my options code.
What gives? Why can't I make 2 while loops in this code? I have seen other people do loops inside of loops, so i'm not sure why this code is failing to execute.
<?php
if (isset($_POST['view_meal'])){
$meal = (string)$_POST['meal_names'];
$meal_fk_q = "SELECT item
FROM meal_ingredients
WHERE meal_name='$meal'
ORDER BY item";
$meal_fk_c = $conn->query($meal_fk_q);
echo "<div class='view_meal_table_wrapper'>";
while ($row = $meal_fk_c->fetch_assoc()){
$view_ingredient = $row['item'];
echo "<table class='view_meal_table'>
<tr>
<td class='view_meal cell'>$view_ingredient</td>
</tr>
</table>";
}
echo "</div>";
echo "<form action='createmeal.php' method='post'>
<select name='remove_ingredients' placeholder='meals'>
<option disabled selected value> -- Remove Ingredient -- </option>";
while ($row = $meal_fk_c->fetch_assoc()){
$view_ingredient = $row['item'];
echo "<option>" . $view_ingredient . "</option>";
}
echo "</select>
<input type='submit' name='remove_ingredient' value='Remove Ingredient'>";
}
?>