当前位置:Gxlcms > PHP教程 > PHP无限极分类相关代码

PHP无限极分类相关代码

时间:2021-07-01 10:21:17 帮助过:12人阅读

  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

人气教程排行