將資料庫取的分類資料格式化,如:
新聞
--體育新聞
--娛樂新聞
財經
--外匯
--金融
-
類樹
- {
-
- /**原始數據*/
- public $original;
-
- > /**id的鍵名*/
- public $id;
-
- /**父親父id的鍵名*/
- public $parentId;
-
- /**初始化時的id */
- protected $initId;
-
- /**節點的層級*/
- protected $thisLevel = 0;
-
- /**最終樹*/
- protected $tree = array();
-
-
- /**
- *建構子
- +-------------------- - ---------------------
- * @access public
- +------------------ -------------- - ------------------------
- * @param array $original
- * 建構子
- +---------------------------------------- --
- * @access public
- +------------------------------------- -----
- * @param array $original 原始資料
- * @param string $id id的鍵名
- * @param string $parentId 父id的鍵名
- +--- ---------------------------------------
- * @return void
-
- * @param string $id
- * 初始化
- +----------------------------------------- -
- * @access public
- +-------------------------------------- ----
- * @param array $original 原始資料
- * @param string $id id的鍵名
- * @param string $parentId 父id的鍵名
- +---- --------------------------------------
- * @return void
-
- * @param string $parentId 父
- * 取得初始節點
- +--------------------------------------- -------
- * @access protected
- +-------------------------------- --------------
- * @param int $parentId 初始節點的等級
- +------------------- ---------------------------
- * @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
- * 取得子樹
- +--------------------------------------- -------
- * @access protected
- +-------------------------------- --------------
- * @param int $id 節點的id
- * @param string $levelTag 縮排標記
- +-------- --------------------------------------
- * @return void
-
- * @param string $id
- * 取得節點的等級
- +-------------------------------------- -----------
- * @access protected
- +---------------------------- ---------------------
- * @param int $parentId 節點的父id
- +------------ -------------------------------------
- * @return void
-
- * @param string $parentId 父親
- * 取得完整的樹
- +-------------------------------------- -----------
- * @access public
- +---------------------------- ---------------------
- * @param int $level 從哪個等級開始取得
- * @param string $levelTag 縮排標記
- +-------------------------------------------------
- * @return array $this->tree 完整的樹
-
- +--------------- ---------------------------------------------------------------- ---
- * @return void
- */
- 公共函數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();
-
- for$($this-> 原為$ key=>$value)
- {
- if($value[$this->parentId] == $parentId)
- {
- array_push($ ParentTree,$value);
- }
- }
-
- return $parentTree;
- }
-
-
- /***/
- 受保護函數getChildrenTree($id,,$level)
- {
- foreach($this->原為$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->原為$key=>$value) { if($parentId == $value[ $this->id] && $ parentId ! = $this->initId) { $this->thisLevel++; $this->getLevel($value[$this-> ;parentId]); } } } /***/ public function getTree($parentId=0,$levelTag=' ') { $this-this- >initId = $parentId; $parentTree = $this->getParentTree($parentId); foreach($parentTree as $key= >$value) { $this- >tree [] = $value; $this->getChildrenTree($value[$this->id],$levelTag); } return $this->tree; } return $this->tree; } }
-
-
-
-
- $conf = array(
- 1 => array('id'=>'1','parentid'=>0,'name' =>' 1'),
- 2 => array('id'=>'2','parentid'=>0,'name'=>'2'),
- 3 => 群集('id'= >'3','parentid'=>1,'name'=>'1-1'),
- 4 => 備份('id'=> ;'4','parentid'=>1,' name'=>'1-2'),
- 5 => array('id'=>'5','parentid'=> ;2,'name'=>'2-1'),
- 6 => 陣列('id'=>'6','parentid'=>3,'name'=>'1-1-1'),
- 7 => 陣列('id'=>' 7','parentid'=>4,'name'=>'1-2-1'),
- 8 => 備份('id'=>'8','parentid'=>5,'name '=>'2-1-1'),
- 9 => 備份('id'=>'9','parentid'=>8,'name'=>'2-1-1-1' )
- );
-
- $tree = 新樹($conf,'id','parentid');
- $arr = $tree->getTree(0,' ');
- foreach($arr as $val)
- {
- if($val['levelTag'])
- {
- echo $val['levelTag'].'|- ';
- }
- echo $val['name'].'
';
- }
-
- ?>
複製程式碼
-
類樹
- {
-
- /**原始數據*/
- public $original;
-
- > /**id的鍵名*/
- public $id;
-
- /**父親父id的鍵名*/
- public $parentId;
-
- /**初始化時的id */
- protected $initId;
-
- /**節點的層級*/
- protected $thisLevel = 0;
-
- /**最終樹*/
- protected $tree = array();
-
-
- /**
- *建構子
- +-------------------- - ---------------------
- * @access public
- +------------------ -------------- - ------------------------
- * @param array $original
- * 建構子
- +---------------------------------------- --
- * @access public
- +------------------------------------- -----
- * @param array $original 原始資料
- * @param string $id id的鍵名
- * @param string $parentId 父id的鍵名
- +--- ---------------------------------------
- * @return void
-
- * @param string $id
- * 初始化
- +----------------------------------------- -
- * @access public
- +-------------------------------------- ----
- * @param array $original 原始資料
- * @param string $id id的鍵名
- * @param string $parentId 父id的鍵名
- +---- --------------------------------------
- * @return void
-
- * @param string $parentId 父
- * 取得初始節點
- +--------------------------------------- -------
- * @access protected
- +-------------------------------- --------------
- * @param int $parentId 初始節點的等級
- +------------------- ---------------------------
- * @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
- * 取得子樹
- +--------------------------------------- -------
- * @access protected
- +-------------------------------- --------------
- * @param int $id 節點的id
- * @param string $levelTag 縮排標記
- +-------- --------------------------------------
- * @return void
-
- * @param string $id
- * 取得節點的等級
- +-------------------------------------- -----------
- * @access protected
- +---------------------------- ---------------------
- * @param int $parentId 節點的父id
- +------------ -------------------------------------
- * @return void
-
- * @param string $parentId 父親
- * 取得完整的樹
- +-------------------------------------- -----------
- * @access public
- +---------------------------- ---------------------
- * @param int $level 從哪個等級開始取得
- * @param string $levelTag 縮排標記
- +-------------------------------------------------
- * @return array $this->tree 完整的樹
-
- +--------------- ---------------------------------------------------------------- ---
- * @return void
- */
- 公共函數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();
-
- for$($this-> 原為$ key=>$value)
- {
- if($value[$this->parentId] == $parentId)
- {
- array_push($ ParentTree,$value);
- }
- }
-
- return $parentTree;
- }
-
-
- /***/
- 受保護函數getChildrenTree($id,,$level)
- {
- foreach($this->原為$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->原為$key=>$value) { if($parentId == $value[ $this->id] && $ parentId ! = $this->initId) { $this->thisLevel++; $this->getLevel($value[$this-> ;parentId]); } } } /***/ public function getTree($parentId=0,$levelTag=' ') { $this-this- >initId = $parentId; $parentTree = $this->getParentTree($parentId); foreach($parentTree as $key= >$value) { $this- >tree [] = $value; $this->getChildrenTree($value[$this->id],$levelTag); } return $this->tree; } return $this->tree; } }
-
-
-
-
- $conf = array(
- 1 => array('id'=>'1','parentid'=>0,'name' =>' 1'),
- 2 => array('id'=>'2','parentid'=>0,'name'=>'2'),
- 3 => 群集('id'= >'3','parentid'=>1,'name'=>'1-1'),
- 4 => 備份('id'=> ;'4','parentid'=>1,' name'=>'1-2'),
- 5 => array('id'=>'5','parentid'=> ;2,'name'=>'2-1'),
- 6 => 陣列('id'=>'6','parentid'=>3,'name'=>'1-1-1'),
- 7 => 陣列('id'=>' 7','parentid'=>4,'name'=>'1-2-1'),
- 8 => 備份('id'=>'8','parentid'=>5,'name '=>'2-1-1'),
- 9 => 備份('id'=>'9','parentid'=>8,'name'=>'2-1-1-1' )
- );
-
- $tree = 新樹($conf,'id','parentid');
- $arr = $tree->getTree(0,' ');
- foreach($arr as $val)
- {
- if($val['levelTag'])
- {
- echo $val['levelTag'].'|- ';
- }
- echo $val['name'].'
';
- }
-
- ?>
複製程式碼
|