Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1599442
  • 博文数量: 1481
  • 博客积分: 26784
  • 博客等级: 上将
  • 技术积分: 17045
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-12 09:22
文章分类

全部博文(1481)

文章存档

2014年(10)

2013年(353)

2012年(700)

2011年(418)

分类: 系统运维

2012-07-04 09:23:50

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

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


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

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

原文地址:

阅读(362) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~