2013-09-05 2 views
1

prepared statement : 두 개의 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 >&nbsp;Qty.</td> 
<td >&nbsp;Writer</td> 
<td >&nbsp;Status</td> 
<td >&nbsp;</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 >&nbsp;<?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>&nbsp;</td></tr> 
</table> 

답변

0

입니다. 동시에 두 결과 세트를 모두 사용하려면 두 요청에 별도의 변수를 사용하십시오. 즉, while 루프 내의 $stmt을 다른 인스턴스로 바꿉니다.

+0

미안하지만 당신을 못 느끼 겠어. 어떻게 할 수 있니? – tech02

0

표기된 바와 같이, 두 가지 모두에 대해 동일한 값을 사용하고 있습니다. 나는 당신이 무엇을 부르고 있는지를 알지 못한다.

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 루프를 사용할 수도 있습니다. 또한 문제가 해결되면 더 깨끗한 방식으로 문제를 해결할 수 있습니다.

+0

미안하지만 조금 혼란스러워 ... 다른 물건을 어떻게 사용할 수 있는지 말해 주시겠습니까? – tech02

+0

귀하의 질문을 고려하여, 나는 당신이 어딘가에서 라이브러리를 사용하고 있다고 가정하고 이것은 코드입니다. 나는 당신의 코드에서 '$ stmt-> fetch()'객체가 무엇인지 또는 어디에서 왔는지를 볼 수 없다. 새 줄에 'print_r ($ stmt-> fetch());'을 출력하는 것이 좋습니다. - 그게 어디서 나오는지 보자. – user1641165

+0

그리고 미안하지만, 나는 다른 대상을 사용하는 것이 상당히 실용적이지 않다는 것을 의미했다. 코드를 두 배로 늘릴 수 있습니다. foreach를 사용하는 것이 훨씬 더 좋습니다. (내 대답을 편집했습니다) – user1641165

관련 문제