PHP無限極分類相關代碼

來源:互聯網
上載者:User
  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
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.