Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Make a tree in smarty

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
whitetulip
Smarty n00b


Joined: 23 Sep 2007
Posts: 2

PostPosted: Sun Sep 23, 2007 10:46 am    Post subject: Make a tree in smarty Reply with quote

Hi all,
I need to know anything about making a tree in smarty,
I just wanna know how to view the nodes in a tree form in the template.
Could anyone help?
Back to top
View user's profile Send private message
lars.koenig
Smarty n00b


Joined: 19 May 2008
Posts: 1

PostPosted: Mon May 19, 2008 7:13 am    Post subject: tree in smarty Reply with quote

Hello,

I am also looking for a tip to create a tree with nodes in smarty.

Thx for an answer

L.
Back to top
View user's profile Send private message
Celeb
Administrator


Joined: 17 Apr 2007
Posts: 1025
Location: Vienna

PostPosted: Mon May 19, 2008 8:55 am    Post subject: Reply with quote

Take a look at this plugin:
http://www.phpinsider.com/smarty-forum/viewtopic.php?p=37934
_________________
Darn computers always do what I tell them to instead of what I want them to do.
Back to top
View user's profile Send private message
student of life
Smarty n00b


Joined: 09 Sep 2008
Posts: 3

PostPosted: Tue Sep 09, 2008 7:21 am    Post subject: Reply with quote

thanks very helpful.
Back to top
View user's profile Send private message
Corith
Smarty n00b


Joined: 05 Mar 2009
Posts: 1

PostPosted: Thu Mar 05, 2009 7:36 pm    Post subject: Reply with quote

I use the one at

pear.php . net / package / HTML_TreeMenu


I created a new class (NodeTree) to allow me to pass mysql data and built the tree dynamically, then dump it to variable.

The following code may seem incomplete. It was designed for a specific need, a list of either checkboxes or radio buttons for hierarchal data, but it might help spur others along.
Code:

$smarty->assign('primaryNodes', DisplayTree() );

function DisplayTree($nodeData)
{
   //$nodeData = "SELECT Node_ID, NodeLabel, ParentID FROM T1"
        $nodetree = new NodeTree();
        $nodetree->ChildControl = 'radio'; //or 'checkbox'
        $nodetree->ChildControlName = 'Nodes[]';
        $nodetree->DataSource = $nodeData;

   //$Checkeddata = "SELECT Node_ID FROM T2 where pk=$thisrecord";
        $nodetree->CheckedData = $CheckedData;
        return $nodetree->DataBind(false);
}



class NodeTree
{
    public $DataSource;
    public $ChildControl;
    public $ChildControlName;
    public $CheckedData;
    public $ShowLinks = false;
   
    public function DataBind($echo = true)
    {

        require_once('TreeMenu.inc');
        $data = $this->DataSource;   
       
        //create an associative array of nodes, key is NodeID, value is HTML_TreeNode object
        $nodelist = Array();
        for($i=0; $i<sizeof($data); $i++)
        {   
            if ($this->ShowLinks)
            $data[$i]['NodeLabel'] = '<a href="node.php?nodeid='.$data[$i]['Node_ID'].'">'.$data[$i]['NodeLabel'].'</a>\n';
           
         
            if ($data[$i]['Node_ID']==$_GET['nodeid'])
            {
                $ChildControl = '';
            }
            else {
           
                switch ($this->ChildControl)
                {
                    case 'checkbox':
                    $checked = ($this->FindCheck($data[$i]['Node_ID'])) ? ' checked ' : '';
                    $ChildControl = '<input type="checkbox" name="'.$this->ChildControlName.'" value="'.$data[$i]['Node_ID'].'" '.$checked.'>&nbsp;';
                     break;
                     
                    case 'radio':
                    $checked = ($this->FindCheck($data[$i]['Node_ID'])) ? ' checked ' : '';
                    $ChildControl = '<input type="radio" name="'.$this->ChildControlName.'" value="'.$data[$i]['Node_ID'].'" '.$checked.'>&nbsp; ';
                     break;
                }
            }
            $nodelist[$data[$i]['Node_ID']] =         
                new HTML_TreeNode (
                    array('text' => $ChildControl.$data[$i]['NodeLabel'], 'link' => '', 'icon' => $icon, 'expandedIcon' => $expandedIcon, 'expanded' => false)
                )
            ;
        }
       
        $icon         = '';
        $pageicon     = '';
        $expandedIcon = '';
           
        $menu  = new HTML_TreeMenu();
       
       
        for($i=0; $i<sizeof($data); $i++)
        {
           
           
           //add top level nodes to top
           if ($data[$i]['ParentID'] == 0 )
           {
                $menu->addItem($nodelist[$data[$i]['Node_ID']]);
           }
           else  //or to a matching parent
           {
                if ($nodelist[$data[$i]['ParentID']])
                    $nodelist[$data[$i]['ParentID']]->addItem($nodelist[$data[$i]['Node_ID']]);

           }
   
        }
       
       // // Create the presentation class
        $treeMenu = &new HTML_TreeMenu_DHTML($menu, array('images' => 'images', 'defaultClass' => 'treeMenuDefault'));

        //and render
       
        if ($echo)
        {
            $options['echo'] = $echo;
            echo '<script src="scripts/TreeMenu.js" language="JavaScript" type="text/javascript"></script>';
            $treeMenu->printMenu($options);
        }
        else
        {
            return '<script src="scripts/TreeMenu.js" language="JavaScript" type="text/javascript"></script>'.$treeMenu->printMenu($options);
        }
           
       
     
    }
   
    private function FindCheck($match)
    {   $returnvalue = false;
        $data = $this->CheckedData;
        for($i=0; $i<sizeof($data); $i++)
        {
            if ($data[$i][0] == $match)
                $returnvalue = true;
        }
        return $returnvalue;
    }
}

Back to top
View user's profile Send private message
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Wed Mar 18, 2009 8:43 pm    Post subject: Another option Reply with quote

Sometimes, you can use this option too:

The websites like dynamic drive (http://www.dynamicdrive.com/) provide enough resources, menus using javascript and css.

Using some simple tweaks in the stored data (array or database), you should be able to feed the javascript the exact way.

By this, nothing becomes harder, but highly customizable too (as you can find the documentation in such sites too).
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP