class baseTree
{
var $path;
var $treeData;
var $treeThtml;
var $treeInfo;
function baseTree(){
$this->treeData = array();
$this->treeThtml = array();
}
function treehtml(){
$this->treeInfo = array();
foreach($this->treeData as $d)
$this->treeInfo[$d['parent_category_id']][] = $d['id'];
foreach($this->treeData as $d)
$this->treeThtml[$d['id']] = $d['category_name'];
ksort($this->treeInfo);
reset($this->treeInfo);
$this->treeInfo = $this->f1($this->treeInfo);
$this->treeInfo = $this->treeInfo[0];
$this->treeInfo = $this->builder($this->treeInfo);
}
function f1($t){
$rv = array();
foreach($t as $d){
if(is_array($d))
$rv[] = $this->f1($d);
else{
if(isset($this->treeInfo[$d])){
$rv[] = array(
'Node' => $d,
'subt' => $this->f1($this->treeInfo[$d])
);
}else
$rv[] = $d;
}
}
return $rv;
}
function builder($t){
$c = '';
foreach($t as $d){
if(is_array($d))
$c .= '
' . $this->treeThtml[$d['Node']] . '' . $this->builder($d['subt']) . '
';
else
$c .= '' . $this->treeThtml[$d] . '';
}
return $c;
}
}
class zpTree extends baseTree
{
var $zp_path;
function zpTree($path){
$this->zp_path = $path;
}
function init(){
echo '
';
}
function activate($handlerJS){
$this->treehtml();
echo '';
echo '
';
}
}
/* sample implementation */
$tree = new zpTree('phpzp/');
/* the data can be fetched from a database table */
$tree->treeData = array(
array('id'=>1,'category_name'=>'Node 1','parent_category_id'=>0),
array('id'=>2,'category_name'=>'Node 2','parent_category_id'=>0),
array('id'=>3,'category_name'=>'Node 3','parent_category_id'=>0),
array('id'=>4,'category_name'=>'Node 2.1','parent_category_id'=>2),
array('id'=>5,'category_name'=>'Node 2.2','parent_category_id'=>2),
array('id'=>6,'category_name'=>'Node 2.1.1','parent_category_id'=>4),
array('id'=>7,'category_name'=>'Node 2.1.1.1','parent_category_id'=>6),
array('id'=>8,'category_name'=>'Node 4','parent_category_id'=>0),
array('id'=>9,'category_name'=>'Node 4.1','parent_category_id'=>8),
array('id'=>10,'category_name'=>'Node 4.2','parent_category_id'=>8),
array('id'=>11,'category_name'=>'Node 4.3','parent_category_id'=>8),
array('id'=>12,'category_name'=>'Node 4.4','parent_category_id'=>8),
);
$jsCode = '
var c = document.getElementById("console");
if (c != null)
c.innerHTML = "Item with ID:" + item_id + " is currently selected";
';
?>
PHP class to build Zapatec Tree By Jiju
$tree->init(); ?>
$tree->activate($jsCode); ?>
No node selected.