Heim > Backend-Entwicklung > PHP-Tutorial > 格式化节点树的类,写得不好,没有算法,但可以用

格式化节点树的类,写得不好,没有算法,但可以用

WBOY
Freigeben: 2016-07-25 09:09:57
Original
1089 Leute haben es durchsucht
将数据库取的分类数据格式化,如:

新闻
--体育新闻
--娱乐新闻
财经
--外汇
--金融
  1. class tree
  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. * @param string $id id的键名
  22. * @param string $parentId 父id的键名
  23. +------------------------------------------
  24. * @return void
  25. */
  26. public function __construct($original='',$id='',$parentId='')
  27. {
  28. if($original && $id && $parentId)
  29. {
  30. $this->initialize($original,$id,$parentId);
  31. }
  32. }
  33. /**
  34. * 初始化
  35. +------------------------------------------
  36. * @access public
  37. +------------------------------------------
  38. * @param array $original 原始数据
  39. * @param string $id id的键名
  40. * @param string $parentId 父id的键名
  41. +------------------------------------------
  42. * @return void
  43. */
  44. public function initialize($original,$id,$parentId)
  45. {
  46. $this->original = $original;
  47. $this->id = $id;
  48. $this->parentId = $parentId;
  49. }
  50. /**
  51. * 获取初始节点
  52. +----------------------------------------------
  53. * @access protected
  54. +----------------------------------------------
  55. * @param int $parentId 初始节点的级别
  56. +----------------------------------------------
  57. * @return array $parentTree
  58. */
  59. protected function getParentTree($parentId)
  60. {
  61. $parentTree = array();
  62. foreach($this->original as $key=>$value)
  63. {
  64. if($value[$this->parentId] == $parentId)
  65. {
  66. array_push($parentTree,$value);
  67. }
  68. }
  69. return $parentTree;
  70. }
  71. /**
  72. * 获取子树
  73. +----------------------------------------------
  74. * @access protected
  75. +----------------------------------------------
  76. * @param int $id 节点的id
  77. * @param string $levelTag 缩进标记
  78. +----------------------------------------------
  79. * @return void
  80. */
  81. protected function getChildrenTree($id,$levelTag)
  82. {
  83. foreach($this->original as $key=>$value)
  84. {
  85. if($id == $value[$this->parentId])
  86. {
  87. if($levelTag)
  88. {
  89. $this->getLevel($value[$this->parentId]);
  90. $value['levelTag'] = str_repeat($levelTag,$this->thisLevel);
  91. $this->thisLevel = 0;
  92. }
  93. $this->tree[] = $value;
  94. $this->getChildrenTree($value[$this->id],$levelTag);
  95. }
  96. }
  97. }
  98. /**
  99. * 获取节点的级别
  100. +-------------------------------------------------
  101. * @access protected
  102. +-------------------------------------------------
  103. * @param int $parentId 节点的父id
  104. +-------------------------------------------------
  105. * @return void
  106. */
  107. protected function getLevel($parentId)
  108. {
  109. foreach($this->original as $key=>$value)
  110. {
  111. if($parentId == $value[$this->id] && $parentId != $this->initId)
  112. {
  113. $this->thisLevel++;
  114. $this->getLevel($value[$this->parentId]);
  115. }
  116. }
  117. }
  118. /**
  119. * 获取完整的树
  120. +-------------------------------------------------
  121. * @access public
  122. +-------------------------------------------------
  123. * @param int $level 从什么级别开始获取
  124. * @param string $levelTag 缩进标记
  125. +-------------------------------------------------
  126. * @return array $this->tree 完整的树
  127. */
  128. public function getTree($parentId=0,$levelTag='')
  129. {
  130. $this->initId = $parentId;
  131. $parentTree = $this->getParentTree($parentId);
  132. foreach($parentTree as $key=>$value)
  133. {
  134. $this->tree[] = $value;
  135. $this->getChildrenTree($value[$this->id],$levelTag);
  136. }
  137. return $this->tree;
  138. }
  139. }
  140. $conf = array(
  141. 1 => array('id'=>'1','parentid'=>0,'name'=>'1'),
  142. 2 => array('id'=>'2','parentid'=>0,'name'=>'2'),
  143. 3 => array('id'=>'3','parentid'=>1,'name'=>'1-1'),
  144. 4 => array('id'=>'4','parentid'=>1,'name'=>'1-2'),
  145. 5 => array('id'=>'5','parentid'=>2,'name'=>'2-1'),
  146. 6 => array('id'=>'6','parentid'=>3,'name'=>'1-1-1'),
  147. 7 => array('id'=>'7','parentid'=>4,'name'=>'1-2-1'),
  148. 8 => array('id'=>'8','parentid'=>5,'name'=>'2-1-1'),
  149. 9 => array('id'=>'9','parentid'=>8,'name'=>'2-1-1-1')
  150. );
  151. $tree = new tree($conf,'id','parentid');
  152. $arr = $tree->getTree(0,'   ');
  153. foreach($arr as $val)
  154. {
  155. if($val['levelTag'])
  156. {
  157. echo $val['levelTag'].'|- ';
  158. }
  159. echo $val['name'].'
    ';
  160. }
  161. ?>
复制代码
  1. class tree
  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. * @param string $id id的键名
  22. * @param string $parentId 父id的键名
  23. +------------------------------------------
  24. * @return void
  25. */
  26. public function __construct($original='',$id='',$parentId='')
  27. {
  28. if($original && $id && $parentId)
  29. {
  30. $this->initialize($original,$id,$parentId);
  31. }
  32. }
  33. /**
  34. * 初始化
  35. +------------------------------------------
  36. * @access public
  37. +------------------------------------------
  38. * @param array $original 原始数据
  39. * @param string $id id的键名
  40. * @param string $parentId 父id的键名
  41. +------------------------------------------
  42. * @return void
  43. */
  44. public function initialize($original,$id,$parentId)
  45. {
  46. $this->original = $original;
  47. $this->id = $id;
  48. $this->parentId = $parentId;
  49. }
  50. /**
  51. * 获取初始节点
  52. +----------------------------------------------
  53. * @access protected
  54. +----------------------------------------------
  55. * @param int $parentId 初始节点的级别
  56. +----------------------------------------------
  57. * @return array $parentTree
  58. */
  59. protected function getParentTree($parentId)
  60. {
  61. $parentTree = array();
  62. foreach($this->original as $key=>$value)
  63. {
  64. if($value[$this->parentId] == $parentId)
  65. {
  66. array_push($parentTree,$value);
  67. }
  68. }
  69. return $parentTree;
  70. }
  71. /**
  72. * 获取子树
  73. +----------------------------------------------
  74. * @access protected
  75. +----------------------------------------------
  76. * @param int $id 节点的id
  77. * @param string $levelTag 缩进标记
  78. +----------------------------------------------
  79. * @return void
  80. */
  81. protected function getChildrenTree($id,$levelTag)
  82. {
  83. foreach($this->original as $key=>$value)
  84. {
  85. if($id == $value[$this->parentId])
  86. {
  87. if($levelTag)
  88. {
  89. $this->getLevel($value[$this->parentId]);
  90. $value['levelTag'] = str_repeat($levelTag,$this->thisLevel);
  91. $this->thisLevel = 0;
  92. }
  93. $this->tree[] = $value;
  94. $this->getChildrenTree($value[$this->id],$levelTag);
  95. }
  96. }
  97. }
  98. /**
  99. * 获取节点的级别
  100. +-------------------------------------------------
  101. * @access protected
  102. +-------------------------------------------------
  103. * @param int $parentId 节点的父id
  104. +-------------------------------------------------
  105. * @return void
  106. */
  107. protected function getLevel($parentId)
  108. {
  109. foreach($this->original as $key=>$value)
  110. {
  111. if($parentId == $value[$this->id] && $parentId != $this->initId)
  112. {
  113. $this->thisLevel++;
  114. $this->getLevel($value[$this->parentId]);
  115. }
  116. }
  117. }
  118. /**
  119. * 获取完整的树
  120. +-------------------------------------------------
  121. * @access public
  122. +-------------------------------------------------
  123. * @param int $level 从什么级别开始获取
  124. * @param string $levelTag 缩进标记
  125. +-------------------------------------------------
  126. * @return array $this->tree 完整的树
  127. */
  128. public function getTree($parentId=0,$levelTag='')
  129. {
  130. $this->initId = $parentId;
  131. $parentTree = $this->getParentTree($parentId);
  132. foreach($parentTree as $key=>$value)
  133. {
  134. $this->tree[] = $value;
  135. $this->getChildrenTree($value[$this->id],$levelTag);
  136. }
  137. return $this->tree;
  138. }
  139. }
  140. $conf = array(
  141. 1 => array('id'=>'1','parentid'=>0,'name'=>'1'),
  142. 2 => array('id'=>'2','parentid'=>0,'name'=>'2'),
  143. 3 => array('id'=>'3','parentid'=>1,'name'=>'1-1'),
  144. 4 => array('id'=>'4','parentid'=>1,'name'=>'1-2'),
  145. 5 => array('id'=>'5','parentid'=>2,'name'=>'2-1'),
  146. 6 => array('id'=>'6','parentid'=>3,'name'=>'1-1-1'),
  147. 7 => array('id'=>'7','parentid'=>4,'name'=>'1-2-1'),
  148. 8 => array('id'=>'8','parentid'=>5,'name'=>'2-1-1'),
  149. 9 => array('id'=>'9','parentid'=>8,'name'=>'2-1-1-1')
  150. );
  151. $tree = new tree($conf,'id','parentid');
  152. $arr = $tree->getTree(0,'   ');
  153. foreach($arr as $val)
  154. {
  155. if($val['levelTag'])
  156. {
  157. echo $val['levelTag'].'|- ';
  158. }
  159. echo $val['name'].'
    ';
  160. }
  161. ?>
复制代码


Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage