how to change a while sql query loop into an array loop

Posted by Mac Taylor on Stack Overflow See other posts from Stack Overflow or by Mac Taylor
Published on 2010-05-20T20:47:16Z Indexed on 2010/05/20 20:50 UTC
Read the original article Hit count: 134

Filed under:
|
|

hey guys

i record number of queries of my website and in page the below script runs , 40 extra queries added to page .

how can I change this sql connection into a propper and light one

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;
}

i heard , this can be done by changing it into an array , but i don't know how to do so

thanks in advance

© Stack Overflow or respective owner

Related posts about php

Related posts about array