|
Simple tree to manage treeviews in php.
<?
/*
//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;
}
}
?>
Sign up to add your own comment here!
|
|