Heim > Backend-Entwicklung > PHP-Tutorial > PHP无限分类[增强版]

PHP无限分类[增强版]

WBOY
Freigeben: 2016-07-25 08:42:26
Original
865 Leute haben es durchsucht
  1. /**
  2. +------------------------------------------------
  3. * 通用的树型类
  4. +------------------------------------------------
  5. * @author yangyunzhou@foxmail.com
  6. +------------------------------------------------
  7. * @date 2010年11月23日10:09:31
  8. +------------------------------------------------
  9. */
  10. class Tree
  11. {
  12. /**
  13. +------------------------------------------------
  14. * 生成树型结构所需要的2维数组
  15. +------------------------------------------------
  16. * @author yangyunzhou@foxmail.com
  17. +------------------------------------------------
  18. * @var Array
  19. */
  20. var $arr = array();
  21. /**
  22. +------------------------------------------------
  23. * 生成树型结构所需修饰符号,可以换成图片
  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. * 构造函数,初始化类
  36. * @param array 2维数组,例如:
  37. * array(
  38. * 1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目一'),
  39. * 2 => array('id'=>'2','parentid'=>0,'name'=>'一级栏目二'),
  40. * 3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目一'),
  41. * 4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目二'),
  42. * 5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目三'),
  43. * 6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目一'),
  44. * 7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目二')
  45. * )
  46. */
  47. function tree($arr=array())
  48. {
  49. $this->arr = $arr;
  50. $this->ret = '';
  51. return is_array($arr);
  52. }
  53. /**
  54. * 得到父级数组
  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. * 得到子级数组
  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. * 得到当前位置数组
  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. * 得到树型结构
  118. * -------------------------------------
  119. * @author yangyunzhou@foxmail.com
  120. * @param $myid 表示获得这个ID下的所有子级
  121. * @param $str 生成树形结构基本代码, 例如: ""
  122. * @param $sid 被选中的ID, 比如在做树形下拉框的时候需要用到
  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. * 同上一方法类似,但允许多选
  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. * 格式化数组
  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


Verwandte Etiketten:
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