Discuz教程网

PHP无限分类代码,支持数组格式化、直接输出菜单两种方式

[复制链接]
authicon 星火燎原 发表于 2011-5-18 10:50:04 | 显示全部楼层 |阅读模式
一朋友写的PHP无限分类代码,分享给大家,支持数组格式化、直接输出菜单两种方式




代码如下:

  1. <?php
  2. /**
  3. +------------------------------------------------
  4. * 通用的树型类
  5. +------------------------------------------------
  6. * @author yangyunzhou@foxmail.com
  7. +------------------------------------------------
  8. * @date 2010年11月23日10:09:31
  9. +------------------------------------------------
  10. */
  11. class Tree
  12. {

  13. /**
  14. +------------------------------------------------
  15. * 生成树型结构所需要的2维数组
  16. +------------------------------------------------
  17. * @author yangyunzhou@foxmail.com
  18. +------------------------------------------------
  19. * @var Array
  20. */
  21. var $arr = array();

  22. /**
  23. +------------------------------------------------
  24. * 生成树型结构所需修饰符号,可以换成图片
  25. +------------------------------------------------
  26. * @author yangyunzhou@foxmail.com
  27. +------------------------------------------------
  28. * @var Array
  29. */
  30. var $icon = array('│','├',' └');

  31. /**
  32. * @access private
  33. */
  34. var $ret = '';

  35. /**
  36. * 构造函数,初始化类
  37. * @param array 2维数组,例如:
  38. * array(
  39. * 1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目一'),
  40. * 2 => array('id'=>'2','parentid'=>0,'name'=>'一级栏目二'),
  41. * 3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目一'),
  42. * 4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目二'),
  43. * 5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目三'),
  44. * 6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目一'),
  45. * 7 => array('id'=>'7','parentid'=>3,'name'=>'三级栏目二')
  46. * )
  47. */
  48. function tree($arr=array())
  49. {
  50. $this->arr = $arr;
  51. $this->ret = '';
  52. return is_array($arr);
  53. }

  54. /**
  55. * 得到父级数组
  56. * @param int
  57. * @return array
  58. */
  59. function get_parent($myid)
  60. {
  61. $newarr = array();
  62. if(!isset($this->arr[$myid])) return false;
  63. $pid = $this->arr[$myid]['pid'];
  64. $pid = $this->arr[$pid]['pid'];
  65. if(is_array($this->arr))
  66. {
  67. foreach($this->arr as $id => $a)
  68. {
  69. if($a['pid'] == $pid) $newarr[$id] = $a;
  70. }
  71. }
  72. return $newarr;
  73. }

  74. /**
  75. * 得到子级数组
  76. * @param int
  77. * @return array
  78. */
  79. function get_child($myid)
  80. {
  81. $a = $newarr = array();
  82. if(is_array($this->arr))
  83. {
  84. foreach($this->arr as $id => $a)
  85. {
  86. if($a['pid'] == $myid) $newarr[$id] = $a;
  87. }
  88. }
  89. return $newarr ? $newarr : false;
  90. }

  91. /**
  92. * 得到当前位置数组
  93. * @param int
  94. * @return array
  95. */
  96. function get_pos($myid,&$newarr)
  97. {
  98. $a = array();
  99. if(!isset($this->arr[$myid])) return false;
  100. $newarr[] = $this->arr[$myid];
  101. $pid = $this->arr[$myid]['pid'];
  102. if(isset($this->arr[$pid]))
  103. {
  104. $this->get_pos($pid,$newarr);
  105. }
  106. if(is_array($newarr))
  107. {
  108. krsort($newarr);
  109. foreach($newarr as $v)
  110. {
  111. $a[$v['id']] = $v;
  112. }
  113. }
  114. return $a;
  115. }

  116. /**
  117. * -------------------------------------
  118. * 得到树型结构
  119. * -------------------------------------
  120. * @author yangyunzhou@foxmail.com
  121. * @param $myid 表示获得这个ID下的所有子级
  122. * @param $str 生成树形结构基本代码, 例如: "<option value=\$id \$select>\$spacer\$name</option>"
  123. * @param $sid 被选中的ID, 比如在做树形下拉框的时候需要用到
  124. * @param $adds
  125. * @param $str_group
  126. */
  127. function get_tree($myid, $str, $sid = 0, $adds = '', $str_group = '')
  128. {
  129. $number=1;
  130. $child = $this->get_child($myid);
  131. if(is_array($child)) {
  132. $total = count($child);
  133. foreach($child as $id=>$a) {
  134. $j=$k='';
  135. if($number==$total) {
  136. $j .= $this->icon[2];
  137. } else {
  138. $j .= $this->icon[1];
  139. $k = $adds ? $this->icon[0] : '';
  140. }
  141. $spacer = $adds ? $adds.$j : '';
  142. $selected = $id==$sid ? 'selected' : '';
  143. @extract($a);
  144. $parentid == 0 && $str_group ? eval("\$nstr = "$str_group";") : eval("\$nstr = "$str";");
  145. $this->ret .= $nstr;
  146. $this->get_tree($id, $str, $sid, $adds.$k.' ',$str_group);
  147. $number++;
  148. }
  149. }
  150. return $this->ret;
  151. }

  152. /**
  153. * 同上一方法类似,但允许多选
  154. */
  155. function get_tree_multi($myid, $str, $sid = 0, $adds = '')
  156. {
  157. $number=1;
  158. $child = $this->get_child($myid);
  159. if(is_array($child))
  160. {
  161. $total = count($child);
  162. foreach($child as $id=>$a)
  163. {
  164. $j=$k='';
  165. if($number==$total)
  166. {
  167. $j .= $this->icon[2];
  168. }
  169. else
  170. {
  171. $j .= $this->icon[1];
  172. $k = $adds ? $this->icon[0] : '';
  173. }
  174. $spacer = $adds ? $adds.$j : '';

  175. $selected = $this->have($sid,$id) ? 'selected' : '';
  176. @extract($a);
  177. eval("\$nstr = "$str";");
  178. $this->ret .= $nstr;
  179. $this->get_tree_multi($id, $str, $sid, $adds.$k.' ');
  180. $number++;
  181. }
  182. }
  183. return $this->ret;
  184. }

  185. function have($list,$item){
  186. return(strpos(',,'.$list.',',','.$item.','));
  187. }

  188. /**
  189. +------------------------------------------------
  190. * 格式化数组
  191. +------------------------------------------------
  192. * @author yangyunzhou@foxmail.com
  193. +------------------------------------------------
  194. */
  195. function getArray($myid=0, $sid=0, $adds='')
  196. {
  197. $number=1;
  198. $child = $this->get_child($myid);
  199. if(is_array($child)) {
  200. $total = count($child);
  201. foreach($child as $id=>$a) {
  202. $j=$k='';
  203. if($number==$total) {
  204. $j .= $this->icon[2];
  205. } else {
  206. $j .= $this->icon[1];
  207. $k = $adds ? $this->icon[0] : '';
  208. }
  209. $spacer = $adds ? $adds.$j : '';
  210. @extract($a);
  211. $a['title'] = $spacer.' '.$a['title'];
  212. $this->ret[$a['id']] = $a;
  213. $fd = $adds.$k.' ';
  214. $this->getArray($id, $sid, $fd);
  215. $number++;
  216. }
  217. }

  218. return $this->ret;
  219. }
  220. }
  221. ?>
复制代码







上一篇:PHP 命令行参数详解及应用
下一篇:JS与PHP向函数传递可变参数的区别实例代码
authicon 皆无 发表于 2011-5-18 19:29:32 | 显示全部楼层
不错不错,我喜欢
authicon melody0721 发表于 2011-5-18 23:30:03 | 显示全部楼层
有意思~顶顶 ,继续顶顶。继续顶哦
authicon Cute宝贝儿 发表于 2011-5-19 04:29:32 | 显示全部楼层
支持!好东西,拿走了!
authicon kurt226 发表于 2011-5-19 05:29:52 | 显示全部楼层
不错不错,我喜欢
authicon 82xiaochong911 发表于 2011-5-19 11:29:40 | 显示全部楼层
不错不错,我喜欢
authicon fhiejkl 发表于 2011-5-19 18:31:16 | 显示全部楼层
好耶!功能强大啊
authicon 福倒菜菜子 发表于 2011-5-20 05:29:52 | 显示全部楼层
支持楼主,顶一下
authicon cutiemimi 发表于 2011-5-20 15:30:11 | 显示全部楼层
回贴下载呀
authicon 风中徜徉 发表于 2011-5-21 10:53:08 | 显示全部楼层
顶的就是你
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-6-19 07:57

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表