【问题标题】:while loop didnt workwhile循环不起作用
【发布时间】:2012-11-11 11:33:48
【问题描述】:

大家好,我正在尝试执行 while 循环。

while 循环将遍历所有订单。 它可以工作,但不知何故它不会在订单状态中循环。 例如,订单 4 状态为待处理,但订单 5 状态未显示任何内容。 我怀疑我把括号放错了。

这是我的代码。

<form action="results-action" method="post" enctype="multipart/form-data">
<fieldset>

<table width="600" border="1">
<tr><td><h2>Pending Order</h2></td></tr>
<tr>

<th scope="col">Order ID</th>
<th scope="col">Name</th>
<th scope="col">Address</th>
<th scope="col">Product Name</th>
<th scope="col">Produt Quantity</th>
<th scope="col">Price</th>
<th scope="col">Order status</th>
</tr>

<?php
while ($row = mysqli_fetch_array($result)) {
?>

<tr>
<td><input type="text" value='<?=$row['virtuemart_order_id']?>' name="orderid" id="virtuemart_order_id"></td>
<td><?=$row['first_name']?></td>
<td><?=$row['address_1']?></td>
<td><?=$row['order_item_name']?></td>
<td><?=$row['product_quantity']?></td>
<td><?=$row['product_final_price'] ?></td>
<td><select name="change">
    <?php 
    while ($row2 = mysqli_fetch_array($result2)) {
    ?>
    <option value="<?=$row2['order_status_code'] ?>"><?=$row2['order_status_name'] ?></option>
    <?php
    } //end inner while
    ?>
</td>
</tr>

<?php
} //end outer while
?>

</table>
</fieldset>

<fieldset>
<table>
<tr>
<td><input type="submit" value="Update status" name="update status"> </td>
</tr>
</table>
</fieldset>
</form>

【问题讨论】:

  • $result$result2 是什么?如果您不以某种方式重置$result2,我看不出内部while 怎么会运行不止一次。
  • 顺便说一句你没有结束&lt;select&gt;
  • 这不会解决您当前的问题,但请查看separation of presentation and contentalternative syntax for control structures
  • 也是一个建议。使用join,因为在我看来,这可以通过一个查询来完成。

标签: php loops while-loop


【解决方案1】:

如果您想在内部while 中每次都获得相同的行,您将不得不重置 mysqli 结果集的内部指针。为此,您可以使用mysqli_data_seek()

你最终会得到这样的结果:

while ($row = mysqli_fetch_array($result)) {
    // snip ...
    while ($row2 = mysqli_fetch_array($result2)) {
        // snip
    }
    mysqli_data_seek($result2, 0);
    // snip
}

【讨论】:

    【解决方案2】:

    还请确保&lt;select&gt; 具有唯一的名称,否则您的所有状态都将被错误捕获。

    <select name="change_<?=$row['row_id']?>">
    

    类似的东西。

    【讨论】:

      猜你喜欢
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      • 2015-03-09
      • 2023-03-22
      • 1970-01-01
      • 2022-12-12
      • 2020-03-30
      相关资源
      最近更新 更多