123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 15/11/4
- * Time: 下午5:39
- */
- class class_tree
- {
- private $group_class;
- public function __construct()
- {
- $this->group_class = $this->load_all();
- }
- public function get_info($cid,&$c_1,&$c_2,&$c_3,&$name)
- {
- $c_3 =0;
- $c_1 = 0;
- $c_2 = 0;
- $info = $this->group_class[$cid];
- if(empty($info)) {
- return false;
- }
- $name = $info['gc_name'];
- do {
- $deep = (int)$info['deep'];
- switch($deep)
- {
- case 1: {
- $c_1 = $cid;
- $n_1 = $info['gc_name'];
- break;
- }
- case 2: {
- $c_2 = $cid;
- $n_2 = $info['gc_name'];
- break;
- }
- case 3: {
- $c_3 = $cid;
- $n_3 = $info['gc_name'];
- break;
- }
- }
- $cid = $info['gc_parent_id'];
- $info = $this->group_class[$cid];
- } while($deep > 0);
- return true;
- }
- private function write_deep(&$alldata,&$childs,$deep)
- {
- if(empty($childs)) return;
- foreach($childs as $cid) {
- $alldata[$cid]['deep'] = $deep;
- $childex = &$alldata[$cid]['child'];
- $this->write_deep($alldata, $childex, $deep + 1);
- }
- }
- /**
- * 取分类列表,最多为三级
- *
- * @param int $show_deep 显示深度
- * @param array $condition 检索条件
- * @return array 数组类型的返回结果
- */
- private function load_all()
- {
- $mod_cats = Model('goods_class');
- $class_list = $mod_cats->field('*')->order('gc_parent_id asc,gc_sort asc,gc_id asc')->limit(false)->select();
- $group_class = array();
- foreach($class_list as $val)
- {
- $key = (int)$val['gc_id'];
- if(empty($group_class[$key])) {
- $group_class[$key] = $val;
- $group_class[$key]['child'] = array();
- }
- }
- if(empty($group_class[0])) {
- $group_class[0] = array();
- $group_class[0]['child'] = array();
- }
- foreach($class_list as $val)
- {
- $cid = (int)$val['gc_id'];
- $pcid = (int)$val['gc_parent_id'];
- if($cid == $pcid) continue;
- $parent = &$group_class[$pcid];
- if (!empty($parent)) {
- array_push($parent['child'],$cid);
- }
- }
- $this->write_deep($group_class,$group_class[0]['child'],1);
- return $group_class;
- }
- }
|