Home > Backend Development > PHP Tutorial > PHP unlimited classification [enhanced version]

PHP unlimited classification [enhanced version]

WBOY
Release: 2016-07-25 08:42:26
Original
865 people have browsed it
  1. /**
  2. +------------------------------------------------
  3. * Universal tree class
  4. +----------------------------------------- -------
  5. * @author yangyunzhou@foxmail.com
  6. +-------------------------------- ----------------
  7. *@date November 23, 2010 10:09:31
  8. +----------------- ----------------------------------
  9. */
  10. class Tree
  11. {
  12. /**
  13. +------------------------------------------------
  14. * 2-dimensional array needed to generate tree structure
  15. +------------------------------------ ------------
  16. * @author yangyunzhou@foxmail.com
  17. +-------------------------- --------------------------
  18. * @var Array
  19. */
  20. var $arr = array();
  21. /**
  22. +------------------------------------------------
  23. * The modification symbols required to generate a tree structure can be replaced by pictures
  24. +--------------------------------- ---------------
  25. * @author yangyunzhou@foxmail.com
  26. +------------------------ --------------------------
  27. * @var Array
  28. */
  29. var $icon = array('│','├',' └');
  30. /**
  31. * @access private
  32. */
  33. var $ret = '';
  34. /**
  35. * Constructor, initialize class
  36. * @param array 2-dimensional array, for example:
  37. * array(
  38. * 1 => array('id'=>'1','parentid'=>0,'name '=>'First-level column one'),
  39. * 2 => array('id'=>'2','parentid'=>0,'name'=>'First-level column two' ),
  40. * 3 => array('id'=>'3','parentid'=>1,'name'=>'Second-level column one'),
  41. * 4 => array( 'id'=>'4','parentid'=>1,'name'=>'Second-level column two'),
  42. * 5 => array('id'=>'5', 'parentid'=>2,'name'=>'Second-level column three'),
  43. * 6 => array('id'=>'6','parentid'=>3,'name '=>'Third-level column one'),
  44. * 7 => array('id'=>'7','parentid'=>3,'name'=>'Third-level column two' )
  45. * )
  46. */
  47. function tree($arr=array())
  48. {
  49. $this->arr = $arr;
  50. $this->ret = '';
  51. return is_array($arr);
  52. }
  53. /**
  54. * Get the parent array
  55. * @param int
  56. * @return array
  57. */
  58. function get_parent($myid)
  59. {
  60. $newarr = array();
  61. if(!isset($this->arr[$myid])) return false;
  62. $pid = $this->arr[$myid]['parentid'];
  63. $pid = $this->arr[$pid]['parentid'];
  64. if(is_array($this->arr))
  65. {
  66. foreach($this->arr as $id => $a)
  67. {
  68. if($a['parentid'] == $pid) $newarr[$id] = $a;
  69. }
  70. }
  71. return $newarr;
  72. }
  73. /**
  74. * Get the child array
  75. * @param int
  76. * @return array
  77. */
  78. function get_child($myid)
  79. {
  80. $a = $newarr = array();
  81. if(is_array($this->arr))
  82. {
  83. foreach($this->arr as $id => $a)
  84. {
  85. if($a['parentid'] == $myid) $newarr[$id] = $a;
  86. }
  87. }
  88. return $newarr ? $newarr : false;
  89. }
  90. /**
  91. * Get the current position array
  92. * @param int
  93. * @return array
  94. */
  95. function get_pos($myid,&$newarr)
  96. {
  97. $a = array();
  98. if(!isset($this->arr[$myid])) return false;
  99. $newarr[] = $this->arr[$myid];
  100. $pid = $this->arr[$myid]['parentid'];
  101. if(isset($this->arr[$pid]))
  102. {
  103. $this->get_pos($pid,$newarr);
  104. }
  105. if(is_array($newarr))
  106. {
  107. krsort($newarr);
  108. foreach($newarr as $v)
  109. {
  110. $a[$v['id']] = $v;
  111. }
  112. }
  113. return $a;
  114. }
  115. /**
  116. * ----------------------------------------
  117. * Get tree structure
  118. * --- ----------------------------------
  119. * @author yangyunzhou@foxmail.com
  120. * @param $myid said Get all the children under this ID
  121. * @param $str Generate the basic code of the tree structure, for example: ""
  122. * @param $sid is The selected ID, for example, needed when making a tree drop-down box
  123. * @param $adds
  124. * @param $str_group
  125. */
  126. function get_tree($myid, $str, $sid = 0, $adds = '', $str_group = '')
  127. {
  128. $number=1;
  129. $child = $this->get_child($myid);
  130. if(is_array($child)) {
  131. $total = count($child);
  132. foreach($child as $id=>$a) {
  133. $j=$k='';
  134. if($number==$total) {
  135. $j .= $this->icon[2];
  136. } else {
  137. $j .= $this->icon[1];
  138. $k = $adds ? $this->icon[0] : '';
  139. }
  140. $spacer = $adds ? $adds.$j : '';
  141. $selected = $id==$sid ? 'selected' : '';
  142. @extract($a);
  143. $parentid == 0 && $str_group ? eval("$nstr = "$str_group";") : eval("$nstr = "$str";");
  144. $this->ret .= $nstr;
  145. $this->get_tree($id, $str, $sid, $adds.$k.' ',$str_group);
  146. $number++;
  147. }
  148. }
  149. return $this->ret;
  150. }
  151. /**
  152. *Similar to the previous method, but allows multiple selections
  153. */
  154. function get_tree_multi($myid, $str, $sid = 0, $adds = '')
  155. {
  156. $number=1;
  157. $child = $this->get_child($myid);
  158. if(is_array($child))
  159. {
  160. $total = count($child);
  161. foreach($child as $id=>$a)
  162. {
  163. $j=$k='';
  164. if($number==$total)
  165. {
  166. $j .= $this->icon[2];
  167. }
  168. else
  169. {
  170. $j .= $this->icon[1];
  171. $k = $adds ? $this->icon[0] : '';
  172. }
  173. $spacer = $adds ? $adds.$j : '';
  174. $selected = $this->have($sid,$id) ? 'selected' : '';
  175. @extract($a);
  176. eval("$nstr = "$str";");
  177. $this->ret .= $nstr;
  178. $this->get_tree_multi($id, $str, $sid, $adds.$k.' ');
  179. $number++;
  180. }
  181. }
  182. return $this->ret;
  183. }
  184. function have($list,$item){
  185. return(strpos(',,'.$list.',',','.$item.','));
  186. }
  187. /**
  188. +------------------------------------------------
  189. * Format array
  190. +---------------------------------------------- -----
  191. * @author yangyunzhou@foxmail.com
  192. +---------------------------------- ---------------
  193. */
  194. function getArray($myid=0, $sid=0, $adds='')
  195. {
  196. $number=1;
  197. $child = $this->get_child($myid);
  198. if(is_array($child)) {
  199. $total = count($child);
  200. foreach($child as $id=>$a) {
  201. $j=$k='';
  202. if($number==$total) {
  203. $j .= $this->icon[2];
  204. } else {
  205. $j .= $this->icon[1];
  206. $k = $adds ? $this->icon[0] : '';
  207. }
  208. $spacer = $adds ? $adds.$j : '';
  209. @extract($a);
  210. $a['name'] = $spacer.' '.$a['name'];
  211. $this->ret[$a['id']] = $a;
  212. $fd = $adds.$k.' ';
  213. $this->getArray($id, $sid, $fd);
  214. $number++;
  215. }
  216. }
  217. return $this->ret;
  218. }
  219. }
  220. ?>
复制代码

  1. 用法:
  2. $tree = new Tree; // new 之前请记得包含tree文件!
  3. $tree->tree($data); // 数据格式请参考 tree方法上面的注释!
  4. // 如果使用数组, 请使用 getArray方法
  5. $tree->getArray();
  6. // 下拉菜单选项使用 get_tree方法
  7. $tree->get_tree();
复制代码

增强版, PHP


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template