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