CI是什么?
CodeIgniter是一個(gè)輕量級(jí)但功能強(qiáng)大的PHP框架,基于MVC設(shè)計(jì)模式,提供了一套豐富的類庫(kù),簡(jiǎn)單易學(xué),高效實(shí)用。
下面看下CI框架無(wú)限級(jí)分類+遞歸的實(shí)現(xiàn)代碼,具體代碼如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
//無(wú)級(jí)分類+遞歸 public function digui(){ $crr = $this ->db->get( 'category' )->result_array(); $list [ 'type' ] = $this ->nolimit( $crr ,0,0); $this ->load->view( 'list1' , $list ); } public function nolimit( $crr , $p_id , $level ){ static $arr = array (); foreach ( $crr as $v ){ if ( $v [ 'parent_id' ]== $p_id ){ $v [ 'level' ] = $level ; $arr [] = $v ; $this ->nolimit( $crr , $v [ 'cat_id' ], $level +1); } } return $arr ; } <td><?PHP echo str_repeat ( ' ' , $val [ 'level' ])?><?php echo $val [ 'cat_name' ]?></td> //獲取1級(jí)、2級(jí)、3級(jí)分類 public function sel_child( $p_id ){ $arr = $this ->sel_son( $p_id ); foreach ( $arr as $k => $v ){ $tmp = $this ->sel_son( $v [ 'cat_id' ]); foreach ( $tmp as $kk => $vv ){ $tmp2 = $this ->sel_son( $vv [ 'cat_id' ]); $tmp [ $kk ][ 'childs' ] = $tmp2 ; } $arr [ $k ][ 'child' ] = $tmp ; } return $arr ; } //通過(guò)ID獲取所有的下級(jí)分類 public function sel_son( $id ){ $this ->db->where( "parent_id=$id" ); return $this ->db->get(self:: $cate )->result_array(); } //渲染展示主頁(yè)模板 public function lists(){ $p_id = 0; $brr [ 'type' ] = $this ->Home_model->sel_child( $p_id ); $brr [ 'list' ] = $this ->db->get( 'goods' )->result_array(); $this ->load->view( 'Home/list.html' , $brr ); } <?php foreach ( $type as $v ){?> <li id= "cat_1" class = "" > <h3><a href= "" ><?php echo $v [ 'cat_name' ]?></a></h3> <?php foreach ( $v [ 'child' ] as $vv ){?> <dl class = "clearfix" > <dt><a href= "" ><?php echo $vv [ 'cat_name' ]?></a></dt> <?php foreach ( $vv [ 'childs' ] as $vvv ){?> <a href= "" ><?php echo $vvv [ 'cat_name' ]?></a> <?php }?> </dl> <?php }?> </li> <?php }?> |
以上所述是小編給大家介紹的CI框架無(wú)限級(jí)分類+遞歸的實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!