tags:

views:

494

answers:

2

hey guys

i need to show a treeview of my categories , saved in my mysql database .

Database table :

table : cats :

columns: id,name,parent problem is in php part :

//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

problem is in php part of my codes that i mentioned . 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 ?

+2  A: 

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

Nalum
can you show an example of the html that you are generating?
Nalum
:D thanks your sloution was totally correct , but current problem is that , this script will add 23 queries to total queries of page , just concider i have 10 rows in table . what if someone has 40 rows then god bless him ! is there any way to get round of this problem !?
Mac Taylor
You could query the table and get all rows set up in an array then loop over the array to create the needed html.
Nalum
i don't get it , would you mind update your answer as an example !?
Mac Taylor
A: 

It really works. But I would like to add items into the tree. and automatically add its row to the database. Does somebody else tried it? I really appreciate it.

Orlin Alvarado