【问题标题】:how to change a while sql query loop into an array loop如何将while sql查询循环更改为数组循环
【发布时间】:2010-05-20 20:47:16
【问题描述】:

我记录了我的网站的查询次数,并在页面中运行以下脚本,向页面添加了 40 个额外的查询。

如何将此 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;

听说,可以改成数组,但是不知道怎么做

提前致谢

【问题讨论】:

    标签: php mysql arrays


    【解决方案1】:

    试试这个(未经测试的代码):

    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>' : '';
    

    我正在做的是两个查询:一个是获取所有类别(您原来的第一个查询),然后是第二个查询以一举获取所有子类别计数。我还将所有类别存储在一个数组中,您可以循环访问,而不是在从数据库中获取时显示。

    【讨论】:

    • 谢谢你,但是在执行你的代码后,appache因为查询溢出而停止工作
    • @Mac:抱歉,我遇到了一个错误,请see the changes I made 告诉我是否可以解决错误。
    • @Mac:哦。愚蠢的我,我刚刚意识到你的算法是递归的。我的回答完全错误...请确认您看到了此评论,因为我想删除此答案。
    • @josh 再次感谢您的尝试,希望为这个非常重要的问题找到答案
    • @Mac:我正在想办法在尽可能少的查询中做到这一点。我所拥有的将是一个改进,但需要更多的工作。
    【解决方案2】:

    可以通过将数据复制到数组中,然后使用该副本来完成:即

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

    稍后,你可以在 $results 上做任何你想做的操作

    您的代码的主要问题是您将显示逻辑与 SQL 查询混合在一起。

    【讨论】:

    • 我不明白,您介意用更完整的代码更新您的答案吗?!定义 $result[] 后我该怎么办?!
    【解决方案3】:

    在单个查询中选择整棵树,例如“从猫中选择 id、name、parent”。迭代结果,在 PHP 中构建代表你的树的数组,然后使用数组作为源来绘制 HTML

    【讨论】:

      猜你喜欢
      • 2020-08-28
      • 2014-06-18
      • 2018-08-13
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-15
      • 2020-09-17
      相关资源
      最近更新 更多