我记录了我的网站的查询数和页面中的下面的脚本运行,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;
我听说,这可以通过改变它变成一个数组来完成,但我不知道怎么做
在此先感谢
谢谢你,但执行你的代码后,appache停止工作,因为查询溢出 – 2010-05-20 21:19:43
@Mac:对不起,我有一个错误,请[请参阅我所做的更改](http://stackoverflow.com/posts/2877867/修订版),并告诉我是否修复了错误。 – Josh 2010-05-20 21:23:43
@Mac:OH。傻我,我只是意识到你的算法是递归的。我的答案是完全错误的......请承认你看到这个评论,因为我想删除这个答案。 – Josh 2010-05-20 21:34:22