php - 如何将while sql查询循环更改为数组循环

标签 php mysql arrays

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

我怎样才能把这个sql连接改成一个propper and light

    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;

我听说,这可以通过将其更改为数组来完成,但我不知道该怎么做

提前致谢

最佳答案

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

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

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

关于php - 如何将while sql查询循环更改为数组循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2877681/

相关文章:

php - 在 MYSQL 数据库中更新外键时出错,其中来自选择框的错误选项数据用于更新外键

java - 重新分配多个线程使用的对象

arrays - 运行时错误9 : Index out of range when accessing a simple array

mysql - E : Sub-process/usr/bin/dpkg returned an error code (1) while removing mysql completely from ubuntu 20. 04

mysql - sql 上周总计

php - 交付 10 个 Base 64 图像的最佳实践

C++ 指向另一个类的动态指针数组

php - 在 wordpress 中使用 get_header 时在页面顶部添加间距

php - 在 PHP 和 MYsql 中保存引导时间选择器值

php - 获得 10 个最多输入的条目