无限极分类1:

  

 1 public function judeg($id)
 2 {
 3    $rs = Db::name('finance_class') -> field('parent_code') -> where('id',$id) -> select();
 4    $i = 1;
 5    foreach($rs as $k => $v){
 6      if($v['parent_code'] <> 0){
 7        $i += $this -> judeg($v['parent_code']);
 8       }
 9    }
10    return $i;
11 }
12 
13  public function cid($id,$pid)
14  {
15     $w['parent_code'] = $id;
16     $rs = Db::name('finance_class')
17        -> field('id,code,name,parent_code')
18        -> where($w)
19        -> order('code asc')
20        -> select();
21     $str = '';
22     foreach ($rs as $k => $v) {
23         $name = $v['name'];
24         $_id = $v['id'];
25         $cutOff = '';
26         for($i = 0; $i < $this -> judeg($_id); $i++){
27           $cutOff.='-';
28         }
29         if($_id == $pid){
30           $str.='<p>他的id='.$_id.'====他的级别'.$cutOff.$name.'</p>';
31         }else{
32           $str.='<p>他的id='.$_id.'====他的级别'.$cutOff.$name.'</p>';
33         }
34         $str.=$this->cid($_id,$pid);
35     }
36     return $str;
37}
38 public function finance_c()
39 {
40    $w['type'] = '资产类';
41    $w['parent_code'] = 0;
42    $rs = Db::name('finance_class')
43        -> field('id,code,name,parent_code')
44        -> where($w)
45         -> select();
46    $str = '';
47    foreach ($rs as $k => $v){
48      $str.= '<p>一级name:'.$v['name'].'</p>';
49      $str.=    $this -> cid($v['id'],0);
50    }
51    echo $str;
52 }

  这个方法出来的效率慢,而且很绕。

方法2:

 1 public function getVoucherClass()
 2 {
 3     $lists = Db::name('finance_class')->select();
 4 
 5     $lists = $this->getTree($lists);
 6 
 7     foreach($lists as $value){
 8       echo str_repeat('--', $value['level']), $value['name'].'<br />';
 9     }
10}
11 
12 /**
13 * 递归实现无限极分类
14 * @param $array 分类数据
15 * @param $pid 父ID
16 * @param $level
17 * @return $list 
18 */
19 function getTree($array, $pid =0, $level = 0){
20    static $list = [];
21    foreach ($array as $key => $value){
22      if ($value['parent_code'] == $pid){
23         $value['level'] = $level;
24          $list[] = $value;
25          unset($array[$key]);
26          $this->getTree($array, $value['id'],$level+1);
27       }
28    }
29    return $list;
30}

无限极分类3:

 1    public function index()
 2     {
 3        $lists = \think\Db::table('ozyx_finance_class')->select();
 4        $lists = $this->getTree($lists,0);
 5 
 6        foreach ($lists as $k => $v) {
 7                $lists_one[$v['type']][]=$v;
 8        }
 9 
10        // halt($lists_one);
11        $this->assign('lists', $lists_one);
12 
13        return view();
14     }
15 
16     /**
17     * 无限极分类
18     */
19     function getTree($data, $pid)
20     {
21         $tree = '';
22         foreach($data as $k => $v)
23         {
24           if($v['parent_code'] == $pid)
25           {        
26                $v['parent_code'] = $this->getTree($data, $v['id']);
27                $tree[] = $v;
28                   unset($data[$k]);
29           }
30         }
31         return $tree;
32     }

 

 

数据表结构:

 

posted on 2018-10-27 10:38  Cc_Pz  阅读(435)  评论(0编辑  收藏  举报