2017-02-10 172 views
0

我在我的php代碼中有2個while循環。第一個以按鈕/桌子格式打印膳食成分用於顯示。第二個while循環將成分放入列表中進行選擇。但是,當我運行我的代碼時,只有第一個while循環運行成功,而第二個循環不成功。PHP:第二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'>"; 
    } 
?> 
+4

您的循環不是嵌套的,他們按順序運行。第一個循環耗盡了結果,所以沒有人留下第二個結果。 – fafl

回答

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

在這裏的所有評論中,您的答案是最簡潔和易於實施的。我也試過你的代碼/版本,它工作。感謝幫助和其他人。 – Shawn

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

您傳遞給$meal_fk_c->fetch_assoc()的資源結果由引用完成。您需要重置指針的位置,然後才能再次使用$meal_fk_c->fetch_assoc()

爲復位位置使用:mysql_data_seek()

或替代的

$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 
}