2011-01-31 156 views
0

我遇到了嵌套的while循環問題。我創建了此功能列出三組,在每個組我想另一個循環列出了該組的四名成員:嵌套WHILE循環的問題

function getBlockCode_MainBody() { 
    global $oSysTemplate; 
    $allGroups = db_res("SELECT g.*, g.id AS ThisID, COUNT(m.id_profile) AS members FROM bx_groups_main AS g LEFT JOIN bx_groups_fans AS m ON g.id = m.id_entry GROUP BY g.id HAVING members >= 1 ORDER BY RAND() LIMIT 3"); 
    $i = 0; 
    while((true == ($groups = mysql_fetch_assoc($allGroups))) && ($i < 3)) { 
     $gid = $groups['ThisID']; 
     $members = db_res("SELECT * FROM bx_groups_fans WHERE id_entry = {$gid}"); 
     while($member = mysql_fetch_assoc($members)) { 
      $gid = $member['id_profile']; 
      $mKeys[] = array(
      'thumbnail' => $gid, 
     ); 
     } 
     $gKeys[] = array(
      'title' => $groups['title'], 
      'gid' => $groups['id'], 
      'bx_repeat:members' => $mKeys, 
     ); 
     $i++; 
    } 
    $aTemplateKeys = array(
     'bx_repeat:groups' => $gKeys, 
    ); 
    return $oSysTemplate -> parseHtmlByName('groups_main.html', $aTemplateKeys); 
} 

它上市的第一環罰款(三組顯示),但是內部的每個組嵌套循環不起作用。我使用會員的ID作爲示例內容。如果有每個組中只有一個用戶,並說他們的成員編號爲1,則輸出:

GROUP 1 - 1 
GROUP 2 - 1 1 
GROUP 3 - 1 1 1 

所以,出於某種原因,嵌套循環繼續運行超過了它應該做的。誰能幫忙?

回答

2

,應清除在每次迭代的$mKeys陣列:

// ... 
while((true == ($groups = mysql_fetch_assoc($allGroups))) && ($i < 3)) { 
    $mKeys = array(); 
    $gid = $groups['ThisID']; 
    // ... 
+0

完美地工作! – hohner 2011-01-31 14:14:58

2

$i++;

嘗試:

unset($mKeys);