Location: PHPKode > scripts > MenuTree > menutree/TreeMenu.php
 * Author: Menno Merlijn
 * Date: 11-jul-2007
 * Version 1.0
 * PHP version > 5
 * Building a tree menu out of some tree nodes
 * Example
$t = new TreeMenu();

//adding the tree nodes
$t->node('lastlegion','movies','The Last Legion','http://www.imdb.com/title/tt0462396/',true,"movie.gif");
$t->node('search','start','Searching media');

//deep tree example
$t->node('node1','start','Node depth 1');
$t->node('node2','node1','Node depth 2');
$t->node('node3','node2','Node depth 3');
$t->node('node4','node3','Node depth 4');
$t->node('node5','node4','Node depth 5');
//making the menu tree


//output the menu tree
echo $t->printTree();

//printing a tree node out of the whole tree
$t->resetTree(); //reset the first tree so we can make a new one
echo $t->printTree();
class TreeMenu{
    //image directory
    const _IMAGE_DIR       = "./image/";
    //image names
    // on changing this names 
    const _IMAGE_DIR_OPEN  = "folder_open.gif";
    const _IMAGE_DIR_CLOSE = "folder_close.gif";
    const _IMAGE_PLUS      = "plus.gif";
    const _IMAGE_MIN       = "min.gif";
    private $nodes=array();
    private $tree;
    private $treeNumber;
    public function __construct(){
    public function resetTree(){
        $this->tree = "";
     * Making tree nodes
     * @param string $name (node name)
     * @param string $parent (parent node)
     * @param string $text (displayed text)
     * @param string $link
     * @param bool $active (visibile child nodes on start)
     * @param string $image (image name)
    public function node($name,$parent,$text,$link=false,$active=true,$image="folder_open.gif"){
     * Recursive function
     * Making a string of all implied nodes
     * @param bool $active  (state of the nodes on start collapsed/open)
     * @param string $node  (name of the starting parent node, could be any node in the tree
     * @param int $margin   (indent of the tree node 
    public function makeTree($active=true,$node='start',$margin=0){
        $class = " class=\"treemenu\"";
        foreach ($this->nodes as $v){
            if($v['name'] == $node){
                    $link = "<a href=\"{$v['link']}\">{$v['text']}</a>";
                    $link = $v['text'];
                //Walk through all nodes looking for a child
                foreach ($this->nodes as $v2){
                    if($v2['ref'] == $node){
                //als het een child node heeft dan...
                    $style = " style=\"margin-left:{$margin}px\"";
                    $id = " id=\"node_{$node}{$this->treeNumber}\"";
                    $onclick = " onclick=\"treemenu('node_{$node}{$this->treeNumber}'".
                    if($active){//child nodes of this node are visible on start
                        //add to treemenu string
                        $this->tree.= "<span{$style}{$id}{$onclick}>".
                                      "<img src=\"".self::_IMAGE_DIR.self::_IMAGE_MIN."\">".
                                      "<img src=\"".self::_IMAGE_DIR.self::_IMAGE_DIR_OPEN."\"{$class}></span>".
                    }else{//child nodes of this nodes are not visible on start
                        //add to treemenu string
                        $this->tree.= "<span{$style}{$id}{$onclick}>".
                                      "<img src=\"".self::_IMAGE_DIR.self::_IMAGE_PLUS."\">".
                                      "<img src=\"".self::_IMAGE_DIR.self::_IMAGE_DIR_CLOSE."\"{$class}></span>".

                }else{//node with no child node
                    //add to treemenu string
                    $this->tree.= "<span style=\"margin-left:".($margin+10)."px\">".
                                  "<img src=\"".self::_IMAGE_DIR."{$v['image']}\"{$class}></span>".
            //visible child node block on start
            $this->tree.= "<div id=\"node_{$node}Blok{$this->treeNumber}\" style=\"display:block\">\n";
            //hidden child node block on start
            $this->tree.= "<div id=\"node_{$node}Blok{$this->treeNumber}\" style=\"display:none;visibility:hidden\">\n";
        //adding the child nodes from this the parent node
        foreach ($this->nodes as $v){
            if($v['ref'] == $node){
                //recursive function call
        //end child node block
        $this->tree.= "</div>\n";
     * returning the menu tree in html code
     * @return string
    public function printTree(){
        return $this->tree;

Return current item: MenuTree