我一直在尋找一段時間的互聯網試圖找到這個答案,但也許我的搜索主題是不正確的。我有一個SQL查詢,它將兩個表中的數據返回到一個組合結果集中。我想循環查找每個「部分」並將其打印出來,並在每個「部分」下方打印出與該部分相關的事件。我基本上有一個父循環尋找「部分」和父循環內的子循環來搜索事件。我的代碼如下。mysqli_fetch_assoc循環內的另一個mysqli_fetch_assoc循環
任何人都可以解釋爲什麼父循環只迭代1次,然後停止?我一定會很感激!
while ($row = mysqli_fetch_assoc($result)) {
if($row['ID'] != "") {
echo '<h3 class="classTlt">'.$row['sTitle'].'</h3>';
echo $row['sDesc'];
$table = '<table cellpadding="0" cellspacing="0" width="100%" border="0">
<tr>';
for($i=1;$i<=6;$i++) {
if($row['sH'.$i] != null) {
$table .= '<th>'.$row['sH'.$i].'</th>';
}
}
$table .= '</tr>';
while ($row2 = mysqli_fetch_assoc($result)) {
if($row2['sID'] == $row['ID']) {
$table .= '<tr>';
for($x=1;$x<=6;$x++) {
if($row2['sV'.$x] != null) {
$table .= '<td valign="top">'.$row2['sV'.$x].'</td>';
}//end if
}//end for
$table .= '</tr>';
}//end if
}//end while
$table .= '</table>';
echo $table;
}//end if
}//end while
說得好。爲你+1。 – Achrome
@scones - 那麼我會如何防止呢?對不起,我還在學習PHP。 – Leann
@scones - 實際上,在兒童循環中,我想從結果集的最開始處開始,而不是第2個記錄。那麼我怎麼能夠使用我的結果集爲兩個循環? – Leann