Format the classified data fetched from the database, such as:
News
--Sports News
--Entertainment News
Finance
--Forex
--Finance
- class tree
- {
- /**Raw data*/
- public $original;
- /**key name of id*/
- public $id;
- /**父The key name of the parent id*/
- public $parentId;
- /**id during initialization*/
- protected $initId;
- /**node level*/
- protected $thisLevel = 0;
- /**final tree*/
- protected $tree = array();
- /**
- * 构造函数
- +------------------------------------------
- * @access public
- +------------------------------------------
- * @param array $original
- * Constructor
- +------------------------------------------------
- * @access public
- +------------------------------------------------
- * @ param array $original original data
- * @param string $id key name of id
- * @param string $parentId key name of parent id
- +------------------- -----------------------
- * @return void
- * @param string $id
- * Initialization
- +------------------------------------------------
- * @ access public
- +------------------------------------------------
- * @param array $original Original data
- * @param string $id The key name of id
- * @param string $parentId The key name of parent id
- +-------------------------- -----------------------
- * @return void
- * @param string $parentId 父
- * Get initial node
- +------------------------------------------------ ---
- * @access protected
- +---------------------------------------- ------
- * @param int $parentId The level of the initial node
- +---------------------------------- ---------------
- * @return array $parentTree
- +------------------------------------------
- * @return void
- */
- public function __construct($original='',$id='',$parentId='')
- {
- if($original && $id && $parentId)
- {
- $this->initialize($original,$id,$parentId);
- }
- }
-
-
- /**
- * 初始化
- +------------------------------------------
- * @access public
- +------------------------------------------
- * @param array $original
- * Get subtree
- +------------------------------------------------ ---
- * @access protected
- +---------------------------------------- ------
- * @param int $id node id
- * @param string $levelTag indentation mark
- +---------------------- --------------------------
- * @return void
-
- * @param string $id
- * Get the level of the node
- +--------------------------------------------- -------
- * @access protected
- +------------------------------------------------ -------------
- * @param int $parentId The parent id of the node
- +------------------------ ------------------------
- * @return void
-
- * @param string $parentId 父
- * Get the complete tree
- +--------------------------------------------- -------
- * @access public
- +------------------------------------------------ -------------
- * @param int $level From what level to get
- * @param string $levelTag Indent tag
- +------------- ------------------------------------
- * @return array $this->tree complete tree
-
- +------------------------------------------
- * @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;
- }
-
-
- /**&*/
- 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]);🎜 }🎜 }🎜 }🎜🎜🎜 /**&*/🎜 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
- {
- /**Raw data*/
- public $original;
- /**key name of id*/
- public $id;
- /**父The key name of the parent id*/
- public $parentId;
- /**id during initialization*/
- protected $initId;
- /**node level*/
- protected $thisLevel = 0;
- /**final tree*/
- protected $tree = array();
- /**
- * 构造函数
- +------------------------------------------
- * @access public
- +------------------------------------------
- * @param array $original
- * Constructor
- +------------------------------------------------
- * @access public
- +------------------------------------------------
- * @ param array $original original data
- * @param string $id key name of id
- * @param string $parentId key name of parent id
- +------------------- -----------------------
- * @return void
- * @param string $id
- * Initialization
- +------------------------------------------------
- * @ access public
- +------------------------------------------------
- * @param array $original Original data
- * @param string $id The key name of id
- * @param string $parentId The key name of parent id
- +-------------------------- -----------------------
- * @return void
- * @param string $parentId 父
- * Get initial node
- +------------------------------------------------ ---
- * @access protected
- +---------------------------------------- ------
- * @param int $parentId The level of the initial node
- +---------------------------------- ---------------
- * @return array $parentTree
- +------------------------------------------
- * @return void
- */
- public function __construct($original='',$id='',$parentId='')
- {
- if($original && $id && $parentId)
- {
- $this->initialize($original,$id,$parentId);
- }
- }
-
-
- /**
- * 初始化
- +------------------------------------------
- * @access public
- +------------------------------------------
- * @param array $original
- * Get subtree
- +------------------------------------------------ ---
- * @access protected
- +---------------------------------------- ------
- * @param int $id node id
- * @param string $levelTag indentation mark
- +---------------------- --------------------------
- * @return void
-
- * @param string $id
- * Get the level of the node
- +--------------------------------------------- -------
- * @access protected
- +------------------------------------------------ -------------
- * @param int $parentId The parent id of the node
- +------------------------ ------------------------
- * @return void
-
- * @param string $parentId 父
- * Get the complete tree
- +--------------------------------------------- -------
- * @access public
- +------------------------------------------------ -------------
- * @param int $level From what level to get
- * @param string $levelTag Indent tag
- +------------- ------------------------------------
- * @return array $this->tree complete tree
-
- +------------------------------------------
- * @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;
- }
-
-
- /**&*/
- 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]);🎜 }🎜 }🎜 }🎜🎜🎜 /**&*/🎜 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'].'
';
- }
-
- ?>
复制代码
|