简单好用的PHP无限分类
2008-08-24 21:32
597 查看
/**
* author: askie
* blog: http://www.pkphp.com
* 版权: 随便用
* 无限分类
*/
class Tree
{
public $data=array();
public $cateArray=array();
function Tree()
{
}
function setNode ($id, $parent, $value)
{
$parent = $parent?$parent:0;
$this->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";
}*/
?>
* author: askie
* blog: http://www.pkphp.com
* 版权: 随便用
* 无限分类
*/
class Tree
{
public $data=array();
public $cateArray=array();
function Tree()
{
}
function setNode ($id, $parent, $value)
{
$parent = $parent?$parent:0;
$this->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";
}*/
?>
相关文章推荐
- php 用地址实现的无限级分类,简单几行
- 比较简单实用的PHP无限分类源码分享(思路不错)
- php 简单实现无限递归分类
- 比较简单实用的PHP无限分类源码分享(思路不错)
- 简单实用的PHP无限分类源码分享
- php简单实现无限分类树形列表的方法
- 比较简单实用的PHP无限分类源码分享(思路不错)
- php 无限级分类,超级简单的无限级分类,支持输出树状图
- PHP如何通过传引用的思想实现无限分类(代码简单)
- php 无限级分类,超级简单的无限级分类,支持输出树状图
- PHP不使用递归的无限级分类简单实例
- mysql和php无限分类 数据归类 输出
- php递归实现无限分类生成下拉列表
- PHP 无限分类三种方式 非函数的递归调用!
- PHPTree――php快速生成无限级分类
- php+mysql实现无限级分类
- PHP递归实现无限级分类并展示
- PHP简单实现无限级分类的方法
- 转:php+mysql菜单无限级分类(非递归)