如何从MySQL和PHP和jquery创建树视图

Mac*_*lor 5 php mysql jquery

我需要显示我的类别的树视图,保存在我的mysql数据库中.

数据库表:

桌子:猫:

:id,name,parent问题在php部分:

//function to build tree menu from db table test1
function tree_set($index)
{
    global $menu;
    $q=mysql_query("select * from cats where parent='$index'");
    if(!mysql_num_rows($q))
        return;
    $menu .= '<ul>'."\n";
    while($arr=mysql_fetch_assoc($q))
    {
        $menu .= '<li>';
        $menu .= '<span class="file">'.$arr['name'].'</span>';//you can add another output there
        $menu .=tree_set("".$arr['id']."");
        $menu .= '</li>'."\n";
    }

    $menu.= '</ul>'."\n";
return $menu;

}



//variable $menu must be defined before the function call
 $menu = '
 <link rel="stylesheet" href="modules/Topics/includes/jquery.treeview.css" />
 <script src="modules/Topics/includes/lib/jquery.cookie.js" type="text/javascript"></script>
 <script src="modules/Topics/includes/jquery.treeview.js" type="text/javascript"></script>
 <script type="text/javascript" src="modules/Topics/includes/demo/demo.js"></script>
 <ul id="browser" class="filetree">'."\n";
 $menu .= tree_set(0);
 $menu .= '</ul>';
echo $menu;  
Run Code Online (Sandbox Code Playgroud)

我甚至在这个论坛上问过:http: //forums.tizag.com/showthread.php?p = 60649

问题是在我提到的我的代码的PHP部分.我不能显示子菜单,我的意思是,我真的不知道如何显示子菜单

有没有机会让专业的PHP编码器帮助我?

Nal*_*lum 3

看起来您正在向菜单变量发送重复的数据,而该变量不需要在那里。

我会改变你的功能来做到这一点:

function tree_set($index)
{
    //global $menu; Remove this.
    $q=mysql_query("select * 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 * 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;
Run Code Online (Sandbox Code Playgroud)

根据对问题的评论进行更新