分享个无限极分类的输出类 包括树状等等

第一个项目使用到了无限极分类,由于前期浪费了太多的时间,做到前台的时候离交项目只有2天多时间了
没时间整理,就随手胡写完成了前台的无限极分类的调用,写完在看这部分实在太乱了,
现在终于有时间了,将用到的整理了一下写了一个类,方便今后的使用
这个是getTree的输出实例只用了一句代码调用
$newssort = $tree->getTree(-1, '<option value="{cid}"{selected}>{spacer}{name}</option>','selected="selected"');

init实例化的时候需要传入数据,如果pid cid不同也要做相应的修改
另外需要注意的是getPosition,因为用到了引用赋值,所以调用方式和其他不同,我已经在注释中说明了
  1. $position=array();
  2. $tree->getPosition(10,$position);
  3. p($position);


getTreeArr这是别人写的代码,其中的引用变量的用法真心强,一个引用,节省了多少代码,连递归什么都省了
其他倒是没有什么,都是一些常用的东西,试一下就知道了,下面是代码

  1. <?php
  2. /*===================================================================
  3. # Program: MCPHP - Web Application Framework
  4. # HomePage: http://www.ptcms.com
  5. # Author: pakey < pakey@qq.com >
  6. # AuthorBlog: http://www.pakey.net
  7. # FileName: tree.class.php
  8. # LastModify: 2012-06-20 21:43:05
  9. # Desc: 无限极分类树形结构
  10. ====================================================================*/
  11. class Tree {
  12. //生成树型结构所需要的2维数组
  13. public $arr = array();
  14. //当前结构数组信息
  15. public $pid; //当前栏目父类id
  16. public $cid; //当前栏目id
  17. public $name; //当前栏目名称
  18. //生成树型结构所需修饰符号,可以换成图片
  19. public $icon = array('┃ ','┣━','┗━'); //栏目分隔符
  20. public $nbsp = ' '; //分隔符 上级栏目与本级栏目之间
  21. /**
  22. * 构造函数,初始化类
  23. * @param array 2维数组,例如:
  24. * array(
  25. * 1 => array('cid'=>'1','parentid'=>0,'name'=>'一级栏目一'),
  26. * 2 => array('cid'=>'2','parentid'=>0,'name'=>'一级栏目二'),
  27. * 3 => array('cid'=>'3','parentid'=>1,'name'=>'二级栏目一'),
  28. * 4 => array('cid'=>'4','parentid'=>1,'name'=>'二级栏目二'),
  29. * 5 => array('cid'=>'5','parentid'=>2,'name'=>'二级栏目三'),
  30. * 6 => array('cid'=>'6','parentid'=>3,'name'=>'三级栏目一'),
  31. * 7 => array('cid'=>'7','parentid'=>3,'name'=>'三级栏目二')
  32. * )
  33. */
  34. public function init ($arr = array(), $name = '根栏目') {
  35. $this->arr = $arr;
  36. $this->ret = '';
  37. $this->pid = isset($this->pid) ? $this->pid : 'parentid';
  38. $this->cid = isset($this->cid) ? $this->cid : 'cid';
  39. $this->name = isset($this->name) ? $this->name : 'name';
  40. //对数组数据修正-》增加根栏目
  41. array_unshift($this->arr, array($this->cid => 0,$this->name => $name,$this->pid => '-1'));
  42. return is_array($arr);
  43. }
  44. /**
  45. * 得到父级栏目的数组
  46. * @param int $myid 当前栏目id
  47. * @return array 父级栏目数组
  48. */
  49. public function getParent ($myid) {
  50. $newArr = array();
  51. if (! isset($this->arr[$myid]))
  52. return false;
  53. // 当前栏目的父id
  54. $pid = $this->arr[$myid][$this->pid];
  55. if (is_array($this->arr)) {
  56. foreach ($this->arr as $id => $v) {
  57. if ($v[$this->cid] == $pid)
  58. $newArr[] = $v;
  59. }
  60. }
  61. return $newArr;
  62. }
  63. /**
  64. * 得到下级栏目的信息数组
  65. * @param int $myid 当前栏目id
  66. * @return array 子级栏目数组
  67. */
  68. public function getChild ($myid) {
  69. $v = $newArr = array();
  70. if (is_array($this->arr)) {
  71. foreach ($this->arr as $id => $v) {
  72. if ($v[$this->pid] == $myid)
  73. $newArr[] = $v;
  74. }
  75. }
  76. return $newArr ? $newArr : false;
  77. }
  78. /**
  79. * 获取下级分类id集合数组
  80. * @param int $myid 当前栏目id
  81. * @param boolen $includeself 是否包含当前栏目id
  82. * @return array 子级栏目id数组
  83. */
  84. public function getChildId ($myid, $includeself = true) {
  85. // 判断是否包含当前栏目id
  86. if ($includeself)
  87. $newArr[] = $myid;
  88. else
  89. $newArr = array();
  90. // 获取下级栏目id集合
  91. if (is_array($this->arr)) {
  92. foreach ($this->arr as $id => $v) {
  93. if ($v[$this->pid] == $myid)
  94. $newArr[] = $v[$this->cid];
  95. }
  96. }
  97. return $newArr ? $newArr : false;
  98. }
  99. /**
  100. * 获取当前栏目的位置数组
  101. * @param int $myid 当前栏目id
  102. * @param array $newArr 返回数组
  103. * 用法:
  104. * $position=array();
  105. * $tree->getPosition(10,$position);
  106. * p($position);
  107. */
  108. public function getPosition ($myid, &$newArr) {
  109. $a = array();
  110. // 数据合法性验证
  111. if (! isset($this->arr[$myid]))
  112. return false;
  113. $newArr[] = $this->arr[$myid];
  114. $pid = $this->arr[$myid][$this->pid];
  115. // 递归获取上级数据
  116. if (isset($this->arr[$pid]))
  117. $this->getPosition($pid, $newArr);
  118. if (is_array($newArr)) {
  119. krsort($newArr);
  120. foreach ($newArr as $v) {
  121. $a[$v['id']] = $v;
  122. }
  123. }
  124. return $a;
  125. }
  126. /**
  127. * 获取某个栏目下栏目数组列表
  128. * @param int $sid 父级栏目id
  129. * @param int $level 下级栏目级别 1为一层下级栏目
  130. * @param string $fieldname 数组下级栏目下标名
  131. * @param int $nowlevel 当前栏目级别
  132. */
  133. public function getList ($sid, $level = 1, $fieldname = 'child', $nowlevel = 0) {
  134. $newArray = $this->getChild($sid);
  135. if (is_array($newArray)) {
  136. ++ $nowlevel;
  137. foreach ($newArray as $id => $value) {
  138. $tmp = $this->getChild($value[$this->cid]);
  139. if ($nowlevel > $level)
  140. $newArray[$id][$fieldname] = $tmp;
  141. else {
  142. foreach ($tmp as $k => $v) {
  143. $tmp[$k][$fieldname] = $this->getList($v[$this->cid], $level, $fieldname, $nowlevel);
  144. }
  145. $newArray[$id][$fieldname] = $tmp;
  146. }
  147. }
  148. }
  149. return $newArray;
  150. }
  151. /**
  152. * 得到树形结构
  153. * @param int $sid 初始栏目id (-1 为从根目录开始)
  154. * @param string $treeStr 生成的结构形式
  155. * 支持参数列表:
  156. * {cid} 当前条目cid 对应于传入数组的下标
  157. * {name} 当前条目name 对应于传入数组的下标
  158. * {url} 当前条目url 对应于传入数组的下标
  159. * {spacer} 分隔符位置
  160. * {selected=***} 选中样式
  161. * 例如
  162. * <option value="{cid}" {selected}>{spacer}{name}</option>
  163. * @param string $selectStr selected的具体样式 selected="selected"
  164. * @param int $myid 当前所在栏目id
  165. * @param string $adds 前置分隔符
  166. * @return string
  167. */
  168. public function getTree ($sid, $treeStr, $selectStr, $myid = 0, $adds = '') {
  169. $number = 1;
  170. $child = $this->getChild($sid);
  171. if (is_array($child)) {
  172. $total = count($child);
  173. foreach ($child as $id => $value) {
  174. $key = $prekey = ''; //分隔符 前置分隔符
  175. if ($number == $total) {
  176. $key .= $this->icon[2];
  177. } else {
  178. $key .= $this->icon[1];
  179. $prekey = $adds ? $this->icon[0] : '';
  180. }
  181. $spacer = $adds ? $adds . $key : '';
  182. // 判断是否选中状态
  183. if ($value[$this->cid] == $myid)
  184. $nstr = str_replace('{selected}', $selectStr, $treeStr);
  185. else
  186. $nstr = str_replace('{selected}', '', $treeStr);
  187. // 替换分隔符
  188. $nstr = str_replace('{spacer}', $spacer, $nstr);
  189. // 替换数组数据
  190. foreach ($value as $k => $v) {
  191. $nstr = str_replace('{' . $k . '}', $v, $nstr);
  192. }
  193. $ret .= $nstr;
  194. $ret .= $this->getTree($value[$this->cid], $treeStr, $selectStr, $myid, $adds . $prekey . $this->nbsp);
  195. $number ++;
  196. }
  197. }
  198. return $ret;
  199. }
  200. /**
  201. * 得到树形结构数组
  202. * @param string $fieldname
  203. * @return array
  204. */
  205. function getTreeArr ($fieldname = 'child') {
  206. $tree = array(); //格式化的树
  207. $tmpMap = array(); //临时扁平数据
  208. foreach ($this->arr as $item) {
  209. $tmpMap[$item[$this->cid]] = $item;
  210. }
  211. foreach ($this->arr as $item) {
  212. if (isset($tmpMap[$item[$this->pid]])) {
  213. $tmpMap[$item[$this->pid]][$fieldname][] = &$tmpMap[$item[$this->cid]];
  214. } else {
  215. $tree[] = &$tmpMap[$item[$this->cid]];
  216. }
  217. }
  218. unset($tmpMap);
  219. return $tree;
  220. }
  221. }
  222. ?>
实例调用
  1. function getlist () {
  2. $tree = new Tree();
  3. $newssorts = $this->db->field('`cid`,`name`,`parentid`')
  4. ->order('`order` asc,`cid` asc')
  5. ->select();
  6. $tree->init($newssorts);
  7. $newssort = $tree->getTree(-1, '<option value="{cid}"{selected}>{spacer}{name}</option>','selected="selected"');
  8. echo '<select>' . $newssort . '</select>';
  9. }

原文地址:http://bbs.lampbrother.net/read-htm-tid-119997.html

<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
阅读(123) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值