准备好的声明:我正在使用两个while循环,但我的内部while循环不起作用。当我使用内部while循环时它只向我显示一条记录,但是当我删除内部while循环时我可以看到所有记录。我可以解决它吗?
这是我的代码
<table width="100%" style="background-color:#F2F2F2;border:1px solid #F2F2F2;border-radius:15px;">
<form name="form1" id="form1" method="post" action="">
<tr>
<td align="">
<table width="100%" style="background-color:#F2F2F2;border:1px solid #F2F2F2;border-radius:15px;">
<tr class="table-heading">
<td > Qty.</td>
<td > Writer</td>
<td > Status</td>
<td > </td>
</tr><?php
if($stmt->prepare("select id,qty,action_flag,status,writer from tbl_order where status=? and action_flag=? and order_id=?"))
{
$action='confirm';
$status='orderprocessing';
$ordid=$_GET["ordid"];
$stmt->bind_param('sss',$status,$action,$ordid);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id,$qty,$action_flag,$status,$writer);
}
while($stmt->fetch()) // outer while loop
{
?>
<tr align="left" style="line-height:30px;font-size:12px;">
<td > <?php echo $qty; ?></td>
<td align="center" width="160" >
<select name="selwrt" id="selwrt" class="reginput" style="text-transform:capitalize;width:150px;height:25px;" >
<option value="">Select Writer</option>
<?php
if($stmt->prepare("select id,writer_name from tbl_writer order by writer_name"))
{
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($wid,$writer_name);
}
while($stmt->fetch()) // inner while loop
{
?>
<option value="<?php echo $wid; ?>"<?php if($writer==$wid) echo 'selected="selected"'; ?>><?php echo $writer_name; ?></option><?php } ?>
</select>
</td>
<td align="center" width="160" ><select name="selst" id="selst" class="reginput" style="text-transform:capitalize;width:150px;height:25px;">
<option value="">Select Status</option>
<option value="inprogress"<?php if($status=='inprogress') echo 'selected="selected"'; ?>>In Progress</option>
<option value="jobdone"<?php if($status=='jobdone') echo 'selected="selected"'; ?>>Job Done</option>
</select>
</td>
<td style="vertical-align:top;" ><input type="submit" name="btnsave" id="btnsave" value="SAVE" class="btnstyle" style="padding:3px;"/>
<input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" ></td>
</tr><?php } ?>
<tr><td> </td></tr>
</table>
答案 0 :(得分:0)
这是因为你在第一次通过循环时用第二个替换初始结果。由于您希望同时使用两个结果集,因此请为这两个请求使用单独的变量 - 即将$stmt
循环内的while
替换为另一个实例。
答案 1 :(得分:0)
正如Mark所说,你对两者使用相同的值。我真的不知道你打的是什么数据,因为它们会发生以下情况。
While (outer loop) {
Starts with first record
While (Inner Loop) {
Goes through all the data
}
Goes back to the top and than realizes that all the data is completed (which was done in the inner loop)
}
2种解决方法。每次使用不同的对象,这可能是相当不切实际的。 (两个单独的方法做同样的事情) 你也可以使用foreach循环。如果它有效,这也将以更清洁的方式解决问题。