简体   繁体   中英

while loop didnt work

Hi guys, I'm trying to do the while loop.

The while loop will loop through all the orders. It works but somehow it doesn't while loop through the order status. For example, order 4 status is pending, but order 5 status shows nothing. I suspect I placed the brackets wrongly.

This is my code.

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

If you want to get the same rows every time in the inner while you will have to reset the internal pointer of the mysqli result set. For this, you can use the mysqli_data_seek()

You will end up with something like this:

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

Please also ensure the <select> has unique name, otherwise all your status will be captured wrongly.

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

Something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM