2010-05-20 159 views
1

我記錄了我的網站的查詢數和頁面中的下面的腳本運行,40個額外的查詢添加到頁面。如何將sql查詢循環更改爲數組循環

我怎樣才能改變這種SQL連接到propper光一個

function tree_set($index) 
    { 
     //global $menu; Remove this. 
     $q=mysql_query("select id,name,parent from cats where parent='$index'"); 
     if(mysql_num_rows($q) === 0) 
     { 
      return; 
     } 

     // User $tree instead of the $menu global as this way there shouldn't be any data duplication 
     $tree = $index > 0 ? '<ul>' : ''; // If we are on index 0 then we don't need the enclosing ul 
     while($arr=mysql_fetch_assoc($q)) 
     { 
      $subFileCount=mysql_query("select id,name,parent from cats where parent='{$arr['id']}'"); 
      if(mysql_num_rows($subFileCount) > 0) 
      { 
       $class = 'folder'; 
      } 
      else 
      { 
       $class = 'file'; 
      } 

      $tree .= '<li>'; 
      $tree .= '<span class="'.$class.'">'.$arr['name'].'</span>'; 
      $tree .=tree_set("".$arr['id'].""); 
      $tree .= '</li>'."\n"; 
     } 
     $tree .= $index > 0 ? '</ul>' : ''; // If we are on index 0 then we don't need the enclosing ul 

     return $tree; 
    } 

//variable $menu must be defined before the function call 
$menu = '....<ul id="browser" class="filetree">'."\n"; 
$menu .= tree_set(0); 
$menu .= '</ul>'; 

echo $menu; 

我聽說,這可以通過改變它變成一個數組來完成,但我不知道怎麼做

在此先感謝

回答

2

試試這個(未測試的代碼):

function tree_set($index) 
{ 
    //global $menu; Remove this. 
    $q=mysql_query("select id,name,parent from cats where parent='$index'"); 
    if(mysql_num_rows($q) === 0) 
     return; 

    $cats = array(); 
    $cat_ids = array(); 

    while($arr=mysql_fetch_assoc($q)) 
    { 
     $id = intval($arr['id']); 
     $cats[$id] = $arr; 
    } 

    $subFilesCountQuery="select parent,count(*) as subFileCount from cats where parent=". 
        join(" OR parent=",array_keys($cats))." GROUP BY parent"; 

    $subFileCountResult=mysql_query($subFilesCountQuery); 

    while($arr=mysql_fetch_assoc($subFileCountResult)) 
    { 
     $id = intval($arr['parent']); 
     $cats[$id]['subFileCount'] = $arr['subFileCount']; 
    } 

    // If we are on index 0 then we don't need the enclosing ul 
    $tree = $index > 0 ? '<ul>' : ''; 
    foreach($cats as $id => $cat) 
    { 
     if($cat['subFileCount'] > 0) 
      $class = 'folder'; 
     else 
      $class = 'file'; 

     $tree .= '<li>'; 
     $tree .= '<span class="'.$class.'">'.$arr['name'].'</span>'; 
     $tree .=tree_set("".$arr['id'].""); 
     $tree .= '</li>'."\n"; 
    } 
    $tree .= $index > 0 ? '</ul>' : ''; 

我在做什麼是兩個查詢:一個獲取所有類別(您的原始第一個查詢),然後進行第二個查詢以一舉獲取所有子類別計數。我還將所有類別存儲在可循環訪問的數組中,而不是在從數據庫中讀取時顯示。

+0

謝謝你,但執行你的代碼後,appache停止工作,因爲查詢溢出 – 2010-05-20 21:19:43

+0

@Mac:對不起,我有一個錯誤,請[請參閱我所做的更改](http://stackoverflow.com/posts/2877867/修訂版),並告訴我是否修復了錯誤。 – Josh 2010-05-20 21:23:43

+0

@Mac:OH。傻我,我只是意識到你的算法是遞歸的。我的答案是完全錯誤的......請承認你看到這個評論,因爲我想刪除這個答案。 – Josh 2010-05-20 21:34:22

0

它可以通過從複製數據到一個數組,然後使用該副本來實現:即

while($arr=mysql_fetch_assoc($q)) 
    { 
     $results[] = $arr; 
    } 

稍後,您隨後在$ results上執行任何您想要的操作

您的代碼的主要問題是您將顯示邏輯全部與SQL查詢混合在一起。

+0

我不明白,你會介意用更完整的代碼更新你的答案嗎?!在定義$ result []後,我應該怎麼做 ? – 2010-05-20 21:16:01

-1

在單個查詢中選擇整棵樹,如「select id,name,parent from cats」。反覆結果,構建PHP數組將代表你的樹,然後使用數組作爲源繪製HTML