PHP:第二个while循环不运行?

时间:2017-02-10 11:00:18

标签: php loops

我的php代码中有2个while循环。第一个以按钮/表格格式打印出餐点的成分以供显示。第二个while循环将成分放入一个列表中进行选择。但是,当我运行我的代码时,只有第一个while循环成功运行而第二个循环不运行。

我尝试颠倒顺序,第一个while循环输入列表,第二个打印出表格到屏幕。我填写了选项菜单,而表格没有打印出来,所以我知道我在编写选项代码时并没有搞砸。

是什么给出的?为什么我不能在此代码中创建2个while循环?我看到其他人在循环中做循环,所以我不确定为什么这段代码无法执行。

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

3 个答案:

答案 0 :(得分:2)

正如评论中所说,首先while循环从您的数据库接收所有记录。这就是为什么在第二个while循环中没有被提取的原因。我建议你将两个循环加入到一个循环中:

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

答案 1 :(得分:0)

首先,结果集完成遍历,并且在第二个while循环中没有剩余数据可以遍历。

解决方案是存储结果集,稍后在foreach循环中使用它。

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

答案 2 :(得分:0)

您传递给$meal_fk_c->fetch_assoc()的资源结果是通过引用完成的。在第二次使用$meal_fk_c->fetch_assoc()之前,您需要重置指针的位置。

要重置位置使用: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   
}