0

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'>";
    }
?>
1
  • 4
    Your loops are not nested, they run sequentially. The first loop exhausts the results, so that none are left for the second. Commented Feb 10, 2017 at 11:01

3 Answers 3

2

As already said in comments, first while loop receives all records from your db. That's why in the second while loop nothing is fetched. I advise you to join both loops into one:

echo "<div class='view_meal_table_wrapper'>";
$option_string = '';   // string with `options`
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>";
     // add `option` markup to string
     $option_string .= "<option>" . $view_ingredient . "</option>";
}
echo "</div>";
echo "<form action='createmeal.php' method='post'>
      <select name='remove_ingredients' placeholder='meals'>
      <option disabled selected value> -- Remove Ingredient -- </option>";
echo $option_string;    // echo options here
echo  "</select>
      <input type='submit' name='remove_ingredient' value='Remove Ingredient'>";
Sign up to request clarification or add additional context in comments.

1 Comment

Out of all the comments here, your answer was the most concise and simple to implement. I also tried your code / version and it worked. Thanks for the help and everyone else too.
0

In first while the result set is completed traversing and there is no data left to traverse in second while loop.

Solution is to store result set and use it later in a foreach loop.

<?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'>";

    $data = array();
    while ($row = $meal_fk_c->fetch_assoc()){
        $data = $row;
        $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>";

        foreach($data as $row)
        {
            $view_ingredient = $row['item'];
            echo "<option>" . $view_ingredient . "</option>";
        }

    echo  "</select>
        <input type='submit' name='remove_ingredient' value='Remove Ingredient'>";
    }
?>

Comments

0

The resource result you pass in to $meal_fk_c->fetch_assoc() is done by reference. You'll need to reset the position of the pointer before you can use $meal_fk_c->fetch_assoc() a second time.

To reset Position use: mysql_data_seek()

OR Alternative

$arrayVals = array();
$result = mysql_query(/* Your query */);
while($row = mysql_fetch_assoc($result)){
    $arrayVals[] = $row;
}

// Now loop over the array twice instead

$len = count($arrayVals);
for($x = 0; $x < $len; $x++) {
    $row = $arrayVals[$x];

    // Do something here    
}

$len = count($arrayVals);
for($x = 0; $x < $len; $x++) {
    $row = $arrayVals[$x];

    // Do something else here   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.