2010-05-20 79 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