简体   繁体   English

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

[英]How to make a tree view from MySQL and PHP and jquery

i need to show a treeview of my categories , saved in my mysql database . 我需要显示我的类别的树视图,保存在我的mysql数据库中。

Database table : 数据库表:

table : cats : 桌子 :猫:

columns : id,name,parent problem is in php part : :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;  

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

problem is in php part of my codes that i mentioned . 问题是在我提到的我的代码的PHP部分。 i cant show sub menus , i mean , really i dont know how to show sub menus 我不能显示子菜单,我的意思是,我真的不知道如何显示子菜单

is there any chance of a pro php coder helping me here ? 有没有机会让专业的PHP编码器帮助我?

It looks like you are sending duplicate data to your menu variable, which doesn't need to be there. 看起来您正在向菜单变量发送重复数据,而不需要在那里。

I would change your function to do this: 我会改变你的功能来做到这一点:

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;

updated based on comments on question 根据对问题的评论进行更新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM