将数据库取的分类数据格式化,如:
新闻
--体育新闻
--娱乐新闻
财经
--外汇
--金融
-
class tree
- {
-
- /**原始数据*/
- public $original;
-
- /**id的键名*/
- public $id;
-
- /**父id的键名*/
- public $parentId;
-
- /**初始化时的id*/
- protected $initId;
-
- /**节点的级别*/
- protected $thisLevel = 0;
-
- /**最终树*/
- protected $tree = array();
-
-
- /**
- * 构造函数
- +------------------------------------------
- * @access public
- +------------------------------------------
- * @param array $original 原始数据
- * @param string $id id的键名
- * @param string $parentId 父id的键名
- +------------------------------------------
- * @return void
- */
- public function __construct($original='',$id='',$parentId='')
- {
- if($original && $id && $parentId)
- {
- $this->initialize($original,$id,$parentId);
- }
- }
-
-
- /**
- * 初始化
- +------------------------------------------
- * @access public
- +------------------------------------------
- * @param array $original 原始数据
- * @param string $id id的键名
- * @param string $parentId 父id的键名
- +------------------------------------------
- * @return void
- */
- public function initialize($original,$id,$parentId)
- {
- $this->original = $original;
- $this->id = $id;
- $this->parentId = $parentId;
- }
-
-
- /**
- * 获取初始节点
- +----------------------------------------------
- * @access protected
- +----------------------------------------------
- * @param int $parentId 初始节点的级别
- +----------------------------------------------
- * @return array $parentTree
- */
- protected function getParentTree($parentId)
- {
- $parentTree = array();
-
- foreach($this->original as $key=>$value)
- {
- if($value[$this->parentId] == $parentId)
- {
- array_push($parentTree,$value);
- }
- }
-
- return $parentTree;
- }
-
-
- /**
- * 获取子树
- +----------------------------------------------
- * @access protected
- +----------------------------------------------
- * @param int $id 节点的id
- * @param string $levelTag 缩进标记
- +----------------------------------------------
- * @return void
- */
- protected function getChildrenTree($id,$levelTag)
- {
- foreach($this->original as $key=>$value)
- {
- if($id == $value[$this->parentId])
- {
- if($levelTag)
- {
- $this->getLevel($value[$this->parentId]);
- $value['levelTag'] = str_repeat($levelTag,$this->thisLevel);
- $this->thisLevel = 0;
- }
- $this->tree[] = $value;
- $this->getChildrenTree($value[$this->id],$levelTag);
- }
- }
- }
-
-
- /**
- * 获取节点的级别
- +-------------------------------------------------
- * @access protected
- +-------------------------------------------------
- * @param int $parentId 节点的父id
- +-------------------------------------------------
- * @return void
- */
- protected function getLevel($parentId)
- {
- foreach($this->original as $key=>$value)
- {
- if($parentId == $value[$this->id] && $parentId != $this->initId)
- {
- $this->thisLevel++;
- $this->getLevel($value[$this->parentId]);
- }
- }
- }
-
-
- /**
- * 获取完整的树
- +-------------------------------------------------
- * @access public
- +-------------------------------------------------
- * @param int $level 从什么级别开始获取
- * @param string $levelTag 缩进标记
- +-------------------------------------------------
- * @return array $this->tree 完整的树
- */
- public function getTree($parentId=0,$levelTag='')
- {
- $this->initId = $parentId;
- $parentTree = $this->getParentTree($parentId);
-
- foreach($parentTree as $key=>$value)
- {
- $this->tree[] = $value;
- $this->getChildrenTree($value[$this->id],$levelTag);
- }
-
- return $this->tree;
- }
-
- }
-
-
-
-
- $conf = array(
- 1 => array('id'=>'1','parentid'=>0,'name'=>'1'),
- 2 => array('id'=>'2','parentid'=>0,'name'=>'2'),
- 3 => array('id'=>'3','parentid'=>1,'name'=>'1-1'),
- 4 => array('id'=>'4','parentid'=>1,'name'=>'1-2'),
- 5 => array('id'=>'5','parentid'=>2,'name'=>'2-1'),
- 6 => array('id'=>'6','parentid'=>3,'name'=>'1-1-1'),
- 7 => array('id'=>'7','parentid'=>4,'name'=>'1-2-1'),
- 8 => array('id'=>'8','parentid'=>5,'name'=>'2-1-1'),
- 9 => array('id'=>'9','parentid'=>8,'name'=>'2-1-1-1')
- );
-
- $tree = new tree($conf,'id','parentid');
- $arr = $tree->getTree(0,' ');
- foreach($arr as $val)
- {
- if($val['levelTag'])
- {
- echo $val['levelTag'].'|- ';
- }
- echo $val['name'].'
';
- }
-
- ?>
复制代码
-
class tree
- {
-
- /**原始数据*/
- public $original;
-
- /**id的键名*/
- public $id;
-
- /**父id的键名*/
- public $parentId;
-
- /**初始化时的id*/
- protected $initId;
-
- /**节点的级别*/
- protected $thisLevel = 0;
-
- /**最终树*/
- protected $tree = array();
-
-
- /**
- * 构造函数
- +------------------------------------------
- * @access public
- +------------------------------------------
- * @param array $original 原始数据
- * @param string $id id的键名
- * @param string $parentId 父id的键名
- +------------------------------------------
- * @return void
- */
- public function __construct($original='',$id='',$parentId='')
- {
- if($original && $id && $parentId)
- {
- $this->initialize($original,$id,$parentId);
- }
- }
-
-
- /**
- * 初始化
- +------------------------------------------
- * @access public
- +------------------------------------------
- * @param array $original 原始数据
- * @param string $id id的键名
- * @param string $parentId 父id的键名
- +------------------------------------------
- * @return void
- */
- public function initialize($original,$id,$parentId)
- {
- $this->original = $original;
- $this->id = $id;
- $this->parentId = $parentId;
- }
-
-
- /**
- * 获取初始节点
- +----------------------------------------------
- * @access protected
- +----------------------------------------------
- * @param int $parentId 初始节点的级别
- +----------------------------------------------
- * @return array $parentTree
- */
- protected function getParentTree($parentId)
- {
- $parentTree = array();
-
- foreach($this->original as $key=>$value)
- {
- if($value[$this->parentId] == $parentId)
- {
- array_push($parentTree,$value);
- }
- }
-
- return $parentTree;
- }
-
-
- /**
- * 获取子树
- +----------------------------------------------
- * @access protected
- +----------------------------------------------
- * @param int $id 节点的id
- * @param string $levelTag 缩进标记
- +----------------------------------------------
- * @return void
- */
- protected function getChildrenTree($id,$levelTag)
- {
- foreach($this->original as $key=>$value)
- {
- if($id == $value[$this->parentId])
- {
- if($levelTag)
- {
- $this->getLevel($value[$this->parentId]);
- $value['levelTag'] = str_repeat($levelTag,$this->thisLevel);
- $this->thisLevel = 0;
- }
- $this->tree[] = $value;
- $this->getChildrenTree($value[$this->id],$levelTag);
- }
- }
- }
-
-
- /**
- * 获取节点的级别
- +-------------------------------------------------
- * @access protected
- +-------------------------------------------------
- * @param int $parentId 节点的父id
- +-------------------------------------------------
- * @return void
- */
- protected function getLevel($parentId)
- {
- foreach($this->original as $key=>$value)
- {
- if($parentId == $value[$this->id] && $parentId != $this->initId)
- {
- $this->thisLevel++;
- $this->getLevel($value[$this->parentId]);
- }
- }
- }
-
-
- /**
- * 获取完整的树
- +-------------------------------------------------
- * @access public
- +-------------------------------------------------
- * @param int $level 从什么级别开始获取
- * @param string $levelTag 缩进标记
- +-------------------------------------------------
- * @return array $this->tree 完整的树
- */
- public function getTree($parentId=0,$levelTag='')
- {
- $this->initId = $parentId;
- $parentTree = $this->getParentTree($parentId);
-
- foreach($parentTree as $key=>$value)
- {
- $this->tree[] = $value;
- $this->getChildrenTree($value[$this->id],$levelTag);
- }
-
- return $this->tree;
- }
-
- }
-
-
-
-
- $conf = array(
- 1 => array('id'=>'1','parentid'=>0,'name'=>'1'),
- 2 => array('id'=>'2','parentid'=>0,'name'=>'2'),
- 3 => array('id'=>'3','parentid'=>1,'name'=>'1-1'),
- 4 => array('id'=>'4','parentid'=>1,'name'=>'1-2'),
- 5 => array('id'=>'5','parentid'=>2,'name'=>'2-1'),
- 6 => array('id'=>'6','parentid'=>3,'name'=>'1-1-1'),
- 7 => array('id'=>'7','parentid'=>4,'name'=>'1-2-1'),
- 8 => array('id'=>'8','parentid'=>5,'name'=>'2-1-1'),
- 9 => array('id'=>'9','parentid'=>8,'name'=>'2-1-1-1')
- );
-
- $tree = new tree($conf,'id','parentid');
- $arr = $tree->getTree(0,' ');
- foreach($arr as $val)
- {
- if($val['levelTag'])
- {
- echo $val['levelTag'].'|- ';
- }
- echo $val['name'].'
';
- }
-
- ?>
复制代码
|