5

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 ?

1
  • 1
    What is your problem, again? You just pasted some code but didn't say which was your problem exactly... I hope you're not expecting us to download the script you mention, understand your problem, and then work for you for free... Commented May 18, 2010 at 13:37

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

3 Comments

: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 !?
You could query the table and get all rows set up in an array then loop over the array to create the needed html.
i don't get it , would you mind update your answer as an example !?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.