首頁 >後端開發 >php教程 >格式化節點樹的類別,寫得不好,沒有演算法,但可以用

格式化節點樹的類別,寫得不好,沒有演算法,但可以用

WBOY
WBOY原創
2016-07-25 09:09:571094瀏覽
將資料庫取的分類資料格式化,如:

新聞
--體育新聞
--娛樂新聞
財經
--外匯
--金融
  1. 類樹
  2. {
  3. /**原始數據*/
  4. public $original;
  5. > /**id的鍵名*/
  6. public $id;
  7. /**父親父id的鍵名*/
  8. public $parentId;
  9. /**初始化時的id */
  10. protected $initId;
  11. /**節點的層級*/
  12. protected $thisLevel = 0;
  13. /**最終樹*/
  14. protected $tree = array();
  15. /**
  16. *建構子
  17. +-------------------- - ---------------------
  18. * @access public
  19. +------------------ -------------- - ------------------------
  20. * @param array $original
  21. * 建構子
  22. +---------------------------------------- --
  23. * @access public
  24. +------------------------------------- -----
  25. * @param array $original 原始資料
  26. * @param string $id id的鍵名
  27. * @param string $parentId 父id的鍵名
  28. +--- ---------------------------------------
  29. * @return void
  30. * @param string $id
  31. * 初始化
  32. +----------------------------------------- -
  33. * @access public
  34. +-------------------------------------- ----
  35. * @param array $original 原始資料
  36. * @param string $id id的鍵名
  37. * @param string $parentId 父id的鍵名
  38. +---- --------------------------------------
  39. * @return void
  40. * @param string $parentId 父
  41. * 取得初始節點
  42. +--------------------------------------- -------
  43. * @access protected
  44. +-------------------------------- --------------
  45. * @param int $parentId 初始節點的等級
  46. +------------------- ---------------------------
  47. * @return array $parentTree
  48. +-------------------- ---------------- - --------
  49. * @return void
  50. */
  51. public function __construct($original='', $id='',$parentId='')
  52. {
  53. if($original && $id && $parentId)
  54. {
  55. $this->initialize($original,$id,$ parentId);
  56. }
  57. }
  58. /**
  59. * 初始化
  60. +------------------ ------------------ -------------- --------
  61. * @access public
  62. +----------------- --------------- -----------
  63. * @param array $original
  64. * 取得子樹
  65. +--------------------------------------- -------
  66. * @access protected
  67. +-------------------------------- --------------
  68. * @param int $id 節點的id
  69. * @param string $levelTag 縮排標記
  70. +-------- --------------------------------------
  71. * @return void
  72. * @param string $id
  73. * 取得節點的等級
  74. +-------------------------------------- -----------
  75. * @access protected
  76. +---------------------------- ---------------------
  77. * @param int $parentId 節點的父id
  78. +------------ -------------------------------------
  79. * @return void
  80. * @param string $parentId 父親
  81. * 取得完整的樹
  82. +-------------------------------------- -----------
  83. * @access public
  84. +---------------------------- ---------------------
  85. * @param int $level 從哪個等級開始取得
  86. * @param string $levelTag 縮排標記
  87. +-------------------------------------------------
  88. * @return array $this->tree 完整的樹
  89. +--------------- ---------------------------------------------------------------- ---
  90. * @return void
  91. */
  92. 公共函數initialize($original,$id,$parentId)
  93. {
  94. $this->original = $original;
  95. $this->id = $ id;
  96. $this->parentId = $parentId;
  97. }
  98. /**
  99. * 取得財富節點
  100. +-- ------ ------------------------------ -----------
  101. * @access protected
  102. +- ------------------------------ ----------- --------------------
  103. * @param int $parentId 初始
  104. +-- ----------- --------------------------------------- --------
  105. * @return array $parentTree
  106. */
  107. protected function getParentTree($parentId)
  108. {
  109. $parentTree = array();
  110. for$($this-> 原為$ key=>$value)
  111. {
  112. if($value[$this->parentId] == $parentId)
  113. {
  114. array_push($ ParentTree,$value);
  115. }
  116. }
  117. return $parentTree;
  118. }
  119. /***/
  120. 受保護函數getChildrenTree($id,,$level)
  121. {
  122. foreach($this->原為$key=>$value)
  123. {
  124. if($id == $value [$this-> parentId])
  125. {
  126. if($levelTag)
  127. {
  128. $this->getLevel($value[$this->parentId]);
  129. $value['levelTag'] = str_repeat($levelTag,$this->thisLevel );
  130. $this->thisLevel = 0;
  131. }
  132. $this->tree[] = $ value;
  133. $this->getChildrenTree( $value[$this->id], $levelTag);
  134. }
  135. }
  136. }
  137. /* *
  138. * 取得 + ---------- ------------------------------------------ -------- -- -- * @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; } }
  139. $conf = array(
  140. 1 => array('id'=>'1','parentid'=>0,'name' =>' 1'),
  141. 2 => array('id'=>'2','parentid'=>0,'name'=>'2'),
  142. 3 => 群集('id'= >'3','parentid'=>1,'name'=>'1-1'),
  143. 4 => 備份('id'=> ;'4','parentid'=>1,' name'=>'1-2'),
  144. 5 => array('id'=>'5','parentid'=> ;2,'name'=>'2-1'),
  145. 6 => 陣列('id'=>'6','parentid'=>3,'name'=>'1-1-1'),
  146. 7 => 陣列('id'=>' 7','parentid'=>4,'name'=>'1-2-1'),
  147. 8 => 備份('id'=>'8','parentid'=>5,'name '=>'2-1-1'),
  148. 9 => 備份('id'=>'9','parentid'=>8,'name'=>'2-1-1-1' )
  149. );
  150. $tree = 新樹($conf,'id','parentid');
  151. $arr = $tree->getTree(0,'   ');
  152. foreach($arr as $val)
  153. {
  154. if($val['levelTag'])
  155. {
  156. echo $val['levelTag'].'|- ';
  157. }
  158. echo $val['name'].'
    ';
  159. }
  160. ?>
複製程式碼
  1. 類樹
  2. {
  3. /**原始數據*/
  4. public $original;
  5. > /**id的鍵名*/
  6. public $id;
  7. /**父親父id的鍵名*/
  8. public $parentId;
  9. /**初始化時的id */
  10. protected $initId;
  11. /**節點的層級*/
  12. protected $thisLevel = 0;
  13. /**最終樹*/
  14. protected $tree = array();
  15. /**
  16. *建構子
  17. +-------------------- - ---------------------
  18. * @access public
  19. +------------------ -------------- - ------------------------
  20. * @param array $original
  21. * 建構子
  22. +---------------------------------------- --
  23. * @access public
  24. +------------------------------------- -----
  25. * @param array $original 原始資料
  26. * @param string $id id的鍵名
  27. * @param string $parentId 父id的鍵名
  28. +--- ---------------------------------------
  29. * @return void
  30. * @param string $id
  31. * 初始化
  32. +----------------------------------------- -
  33. * @access public
  34. +-------------------------------------- ----
  35. * @param array $original 原始資料
  36. * @param string $id id的鍵名
  37. * @param string $parentId 父id的鍵名
  38. +---- --------------------------------------
  39. * @return void
  40. * @param string $parentId 父
  41. * 取得初始節點
  42. +--------------------------------------- -------
  43. * @access protected
  44. +-------------------------------- --------------
  45. * @param int $parentId 初始節點的等級
  46. +------------------- ---------------------------
  47. * @return array $parentTree
  48. +-------------------- ---------------- - --------
  49. * @return void
  50. */
  51. public function __construct($original='', $id='',$parentId='')
  52. {
  53. if($original && $id && $parentId)
  54. {
  55. $this->initialize($original,$id,$ parentId);
  56. }
  57. }
  58. /**
  59. * 初始化
  60. +------------------ ------------------ -------------- --------
  61. * @access public
  62. +----------------- --------------- -----------
  63. * @param array $original
  64. * 取得子樹
  65. +--------------------------------------- -------
  66. * @access protected
  67. +-------------------------------- --------------
  68. * @param int $id 節點的id
  69. * @param string $levelTag 縮排標記
  70. +-------- --------------------------------------
  71. * @return void
  72. * @param string $id
  73. * 取得節點的等級
  74. +-------------------------------------- -----------
  75. * @access protected
  76. +---------------------------- ---------------------
  77. * @param int $parentId 節點的父id
  78. +------------ -------------------------------------
  79. * @return void
  80. * @param string $parentId 父親
  81. * 取得完整的樹
  82. +-------------------------------------- -----------
  83. * @access public
  84. +---------------------------- ---------------------
  85. * @param int $level 從哪個等級開始取得
  86. * @param string $levelTag 縮排標記
  87. +-------------------------------------------------
  88. * @return array $this->tree 完整的樹
  89. +--------------- ---------------------------------------------------------------- ---
  90. * @return void
  91. */
  92. 公共函數initialize($original,$id,$parentId)
  93. {
  94. $this->original = $original;
  95. $this->id = $ id;
  96. $this->parentId = $parentId;
  97. }
  98. /**
  99. * 取得財富節點
  100. +-- ------ ------------------------------ -----------
  101. * @access protected
  102. +- ------------------------------ ----------- --------------------
  103. * @param int $parentId 初始
  104. +-- ----------- --------------------------------------- --------
  105. * @return array $parentTree
  106. */
  107. protected function getParentTree($parentId)
  108. {
  109. $parentTree = array();
  110. for$($this-> 原為$ key=>$value)
  111. {
  112. if($value[$this->parentId] == $parentId)
  113. {
  114. array_push($ ParentTree,$value);
  115. }
  116. }
  117. return $parentTree;
  118. }
  119. /***/
  120. 受保護函數getChildrenTree($id,,$level)
  121. {
  122. foreach($this->原為$key=>$value)
  123. {
  124. if($id == $value) [$this- >parentId])
  125. {
  126. if($levelTag)
  127. {
  128. $this->getLevel($value[$this->parentId]);
  129. $value['levelTag'] = str_repeat($levelTag,$this-> thisLevel);
  130. $this->thisLevel = 0;
  131. }
  132. $this->tree[] = $ value;
  133. $this->getChildrenTree ($value[$this->id] ,$levelTag);
  134. }
  135. }
  136. }
  137. /* *
  138. * 取得 +--------- ------------------------------------------- ------- -- --- * @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; } }
  139. $conf = array(
  140. 1 => array('id'=>'1','parentid'=>0,'name' =>' 1'),
  141. 2 => array('id'=>'2','parentid'=>0,'name'=>'2'),
  142. 3 => 群集('id'= >'3','parentid'=>1,'name'=>'1-1'),
  143. 4 => 備份('id'=> ;'4','parentid'=>1,' name'=>'1-2'),
  144. 5 => array('id'=>'5','parentid'=> ;2,'name'=>'2-1'),
  145. 6 => 陣列('id'=>'6','parentid'=>3,'name'=>'1-1-1'),
  146. 7 => 陣列('id'=>' 7','parentid'=>4,'name'=>'1-2-1'),
  147. 8 => 備份('id'=>'8','parentid'=>5,'name '=>'2-1-1'),
  148. 9 => 備份('id'=>'9','parentid'=>8,'name'=>'2-1-1-1' )
  149. );
  150. $tree = 新樹($conf,'id','parentid');
  151. $arr = $tree->getTree(0,'   ');
  152. foreach($arr as $val)
  153. {
  154. if($val['levelTag'])
  155. {
  156. echo $val['levelTag'].'|- ';
  157. }
  158. echo $val['name'].'
    ';
  159. }
  160. ?>
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn