Source Rally PHP Community Scripts .. Sign up .. Login
Simple tree to manage treeviews in php.
Access: Public      Tags: php, tree, climb, children, structure
Add to favourites       Subscribe comments       Copy code       Bookmark
<?
/*
//example:
$tree = new simpleTree;
$tree->add(0,null);
$tree->add(1,0);
$tree->add(1.1,1);
$tree->add(1.2,1);
$tree->add(2,0);
*/
class simpleTree
{
    public 
$items = array();
    public 
$relations = array();
    
    function 
__construct()
    {
    
    }
    
    
//Add an item...
    
function add($id,$pid)
    {
        
$item = new stdClass;
        
$item->id $id;
        
$item->pid $pid;
        
$this->items[$item->id]=$item;
        if(!isset(
$this->relations[$pid]))
            
$this->relations[$pid] = array();
        
$this->relations[$pid][]=$id;
    }
    
    
//climb to the top of the tree from a certain item.
    
function climb($id,$data = array())
    {
        if(
$id===null)
            return 
$data;
        if(
$data[$id])
            return 
$data;
        
$item $this->items[$id];
        if(!
$item)
            return 
$data;
        
$data[$id]=$item;
        return 
$this->climb($item->pid,$data);
    }
    
    
//Get the children of a certain item.
    
function children($id)
    {
        
$data = array();
        foreach(
$this->relations[$id] as $v)
        {
            
$data[]=$this->items[$v];
        }
        return 
$data;
    }
}
?>
Add to favourites       Subscribe comments       Copy code       Bookmark
Sign up to add your own comment here!

Shared by:

regin

Mail user Add to friends
All user contributed content is available under the LGPL unless specified otherwise.
Remaining copyrights Regin Gaarsmand © 2006-2008
About SourceRally.net
Adelgazar sin trucos Programador PHP