PHP无限极分类相关代码

WBOY
Freigeben: 2016-07-25 08:43:23
Original
996 Leute haben es durchsucht
  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


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!