class_tree.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. if(empty($info)) {
  22. return false;
  23. }
  24. $name = $info['gc_name'];
  25. do {
  26. $deep = (int)$info['deep'];
  27. switch($deep)
  28. {
  29. case 1: {
  30. $c_1 = $cid;
  31. $n_1 = $info['gc_name'];
  32. break;
  33. }
  34. case 2: {
  35. $c_2 = $cid;
  36. $n_2 = $info['gc_name'];
  37. break;
  38. }
  39. case 3: {
  40. $c_3 = $cid;
  41. $n_3 = $info['gc_name'];
  42. break;
  43. }
  44. }
  45. $cid = $info['gc_parent_id'];
  46. $info = $this->group_class[$cid];
  47. } while($deep > 0);
  48. return true;
  49. }
  50. private function write_deep(&$alldata,&$childs,$deep)
  51. {
  52. if(empty($childs)) return;
  53. foreach($childs as $cid) {
  54. $alldata[$cid]['deep'] = $deep;
  55. $childex = &$alldata[$cid]['child'];
  56. $this->write_deep($alldata, $childex, $deep + 1);
  57. }
  58. }
  59. /**
  60. * 取分类列表,最多为三级
  61. *
  62. * @param int $show_deep 显示深度
  63. * @param array $condition 检索条件
  64. * @return array 数组类型的返回结果
  65. */
  66. private function load_all()
  67. {
  68. $mod_cats = Model('goods_class');
  69. $class_list = $mod_cats->field('*')->order('gc_parent_id asc,gc_sort asc,gc_id asc')->limit(false)->select();
  70. $group_class = array();
  71. foreach($class_list as $val)
  72. {
  73. $key = (int)$val['gc_id'];
  74. if(empty($group_class[$key])) {
  75. $group_class[$key] = $val;
  76. $group_class[$key]['child'] = array();
  77. }
  78. }
  79. if(empty($group_class[0])) {
  80. $group_class[0] = array();
  81. $group_class[0]['child'] = array();
  82. }
  83. foreach($class_list as $val)
  84. {
  85. $cid = (int)$val['gc_id'];
  86. $pcid = (int)$val['gc_parent_id'];
  87. if($cid == $pcid) continue;
  88. $parent = &$group_class[$pcid];
  89. if (!empty($parent)) {
  90. array_push($parent['child'],$cid);
  91. }
  92. }
  93. $this->write_deep($group_class,$group_class[0]['child'],1);
  94. return $group_class;
  95. }
  96. }