您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php实现无限级分类

2024/5/12 13:00:39发布32次查看
author: askie blog: http://www.pkphp.com 版权: 随便用 无限分类
data[$id] = $value;   
$this->catearray[$id] = $parent;   
}   
function getchildstree($id=0)   
{   
$childs=array();   
foreach ($this->catearray as $child=>$parent)   
{   
if ($parent==$id)   
{   
$childs[$child]=$this->getchildstree($child);   
}
}   
return $childs;   
}   
function getchilds($id=0)   
{   
$childarray=array();   
$childs=$this->getchild($id);   
foreach ($childs as $child)   
{   
$childarray[]=$child;   
$childarray=array_merge($childarray,$this->getchilds($child));   
}   
return $childarray;   
}   
function getchild($id)   
{   
$childs=array();   
foreach ($this->catearray as $child=>$parent)   
{   
if ($parent==$id)   
{   
$childs[$child]=$child;   
}   
}   
return $childs;   
}   
//单线获取父节点   
function getnodelever($id)   
{   
$parents=array();   
if (key_exists($this->catearray[$id],$this->catearray))   
{   
$parents[]=$this->catearray[$id];   
$parents=array_merge($parents,$this->getnodelever($this->catearray[$id]));   
}   
return $parents;   
}   
function getlayer($id,$prestr=’-’)   
{   
return str_repeat($prestr,count($this->getnodelever($id)));   
}   
function getvalue ($id)   
{   
return $this->data[$id];   
} // end func   
}
$tree = new tree(”请选择分类”);   
//setnode(目录id,上级id,目录名字);   
$tree->setnode(1, 0, ‘目录1′);   
$tree->setnode(2, 1, ‘目录2′);   
$tree->setnode(5, 3, ‘目录5′);   
$tree->setnode(3, 0, ‘目录3′);   
$tree->setnode(4, 2, ‘目录4′);   
$tree->setnode(9, 4, ‘目录9′);   
$tree->setnode(6, 2, ‘目录6′);   
$tree->setnode(7, 2, ‘目录7′);   
$tree->setnode(8, 3, ‘目录8′);
//print_r($tree->getchildstree(0));   
//print_r($tree->getchild(0));   
//print_r($tree->getlayer(2));
$category = $tree->getchilds();
//遍历输出   
foreach ($category as $key=>$id)   
{   
echo $id.$tree->getlayer($id, ‘-’).$tree->getvalue($id).”\n”;   
}
?>
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product