class_tree.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 15/11/4
  6. * Time: 下午5:39
  7. */
  8. class class_tree
  9. {
  10. private $group_class;
  11. public function __construct()
  12. {
  13. $this->group_class = $this->load_all();
  14. }
  15. public function get_info($cid,&$c_1,&$c_2,&$c_3,&$name)
  16. {
  17. $c_3 =0;
  18. $c_1 = 0;
  19. $c_2 = 0;
  20. $info = $this->group_class[$cid];
  21. $name = $info['gc_name'];
  22. do {
  23. $deep = (int)$info['deep'];
  24. switch($deep)
  25. {
  26. case 1: {
  27. $c_1 = $cid;
  28. $n_1 = $info['gc_name'];
  29. break;
  30. }
  31. case 2: {
  32. $c_2 = $cid;
  33. $n_2 = $info['gc_name'];
  34. break;
  35. }
  36. case 3: {
  37. $c_3 = $cid;
  38. $n_3 = $info['gc_name'];
  39. break;
  40. }
  41. }
  42. $cid = $info['gc_parent_id'];
  43. $info = $this->group_class[$cid];
  44. } while($deep > 0);
  45. }
  46. private function write_deep(&$alldata,&$childs,$deep)
  47. {
  48. if(empty($childs)) return;
  49. foreach($childs as $cid) {
  50. $alldata[$cid]['deep'] = $deep;
  51. $childex = &$alldata[$cid]['child'];
  52. $this->write_deep($alldata, $childex, $deep + 1);
  53. }
  54. }
  55. /**
  56. * 取分类列表,最多为三级
  57. *
  58. * @param int $show_deep 显示深度
  59. * @param array $condition 检索条件
  60. * @return array 数组类型的返回结果
  61. */
  62. private function load_all()
  63. {
  64. $mod_cats = Model('goods_class');
  65. $class_list = $mod_cats->field('*')->order('gc_parent_id asc,gc_sort asc,gc_id asc')->limit(false)->select();
  66. $group_class = array();
  67. foreach($class_list as $val)
  68. {
  69. $key = (int)$val['gc_id'];
  70. if(empty($group_class[$key])) {
  71. $group_class[$key] = $val;
  72. $group_class[$key]['child'] = array();
  73. }
  74. }
  75. if(empty($group_class[0])) {
  76. $group_class[0] = array();
  77. $group_class[0]['child'] = array();
  78. }
  79. foreach($class_list as $val)
  80. {
  81. $cid = (int)$val['gc_id'];
  82. $pcid = (int)$val['gc_parent_id'];
  83. if($cid == $pcid) continue;
  84. $parent = &$group_class[$pcid];
  85. if (!empty($parent)) {
  86. array_push($parent['child'],$cid);
  87. }
  88. }
  89. $this->write_deep($group_class,$group_class[0]['child'],1);
  90. return $group_class;
  91. }
  92. }