PHP Infinitus classification related codes

WBOY
Release: 2016-07-25 08:43:23
Original
1033 people have browsed it
  1. //Non-recursively get all descendant categories
  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. //Use the path field to get the descendant classification
  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. //Update descendant classification path
  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. //Organize the list into a tree shape
  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]))//Set the subclass tag
  74. {
  75. $r[ $pid]['has_child'] = 1;
  76. }
  77. $v['cat_path'] = trim($v['cat_path']);//Calculate depth
  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']);/ /Calculate indent
  88. $r[$v['cat_id']] = $v;//Add to ordered list
  89. unset($arr[$k]);//Add and remove from unordered list
  90. $this->get_tree_list($v['cat_id'], $arr, $r);
  91. }
  92. }
  93. }
Copy code

Infinitus, PHP


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!