PHP无限极分类相关代码

WBOY
发布: 2016-07-25 08:43:23
原创
996 人浏览过
  1. //非递归获取所有后代分类
  2. function get_offspring($pids, $list)
  3. {
  4. $npid = array();
  5. $offspring = array();
  6. $has_child = true;
  7. while($has_child)
  8. {
  9. $has_child = false;
  10. foreach($list as $lk => $lv)
  11. {
  12. if(in_array($lv['pid'], $pids))
  13. {
  14. $offspring[] = $lv;
  15. $npid[] = $lv['cid'];
  16. unset($list[$lk]);
  17. $has_child = true;
  18. }
  19. }
  20. $pids = $npid;
  21. }
  22. return $offspring;
  23. }
  24. //利用路径字段获取后辈分类
  25. function get_offspring($pid)
  26. {
  27. $offspring = array();
  28. $cats = $this->getList('cat_id,cat_name,parent_id,cat_path', array(), 0, -1);
  29. foreach($cats as $cv)
  30. {
  31. if(in_array($pid, explode(',', $cv['cat_path'])))
  32. {
  33. $offspring[] = $cv;
  34. }
  35. }
  36. return $offspring;
  37. }
  38. //更新后辈分类路径
  39. function update_offspring_path($pid, $ppath)
  40. {
  41. if($ppath == ',')
  42. {
  43. $ppath = '';
  44. }
  45. $offspring = $this->get_offspring($pid);
  46. foreach($offspring as $ov)
  47. {
  48. $ov['cat_path'] = substr($ov['cat_path'], 0, strlen($ov['cat_path'])-1);
  49. $old_path = explode(',', $ov['cat_path']);
  50. foreach($old_path as $oldk => $oldv)
  51. {
  52. if($oldv == $pid)
  53. {
  54. break;
  55. }
  56. unset($old_path[$oldk]);
  57. }
  58. $new_path = $ppath.implode(',', $old_path).',';
  59. if(!$this->update(array('cat_path'=>$new_path), array('cat_id'=>$ov['cat_id'])))
  60. {
  61. return false;
  62. }
  63. }
  64. return true;
  65. }
  66. //将列表整理为树形
  67. function get_tree_list($pid, $arr, &$r)
  68. {
  69. foreach($arr as $k => $v)
  70. {
  71. if($v['parent_id'] == $pid)
  72. {
  73. if(isset($r[$pid]))//设置含有子类标记
  74. {
  75. $r[$pid]['has_child'] = 1;
  76. }
  77. $v['cat_path'] = trim($v['cat_path']);//计算深度
  78. $path_str = substr($v['cat_path'], 0, strlen($v['cat_path'])-1);
  79. if($path_str == '')
  80. {
  81. $v['deep'] = 0;
  82. }
  83. else
  84. {
  85. $v['deep'] = count(explode(',', $path_str));
  86. }
  87. $v['format'] = str_repeat(' ', 6*$v['deep']);//计算缩进
  88. $r[$v['cat_id']] = $v;//加入有序列表
  89. unset($arr[$k]);//已加入 从无序列表中剔除
  90. $this->get_tree_list($v['cat_id'], $arr, $r);
  91. }
  92. }
  93. }
复制代码

无限极, PHP


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!