CI架構無限級分類+遞迴的實現代碼_php執行個體

來源:互聯網
上載者:User

CI是什麼?

CodeIgniter是一個輕量級但功能強大的PHP架構,基於MVC設計模式,提供了一套豐富的類庫,簡單易學,高效實用。

下面看下CI架構無限級分類+遞迴的實現代碼,具體代碼如下所示:

//無級分類+遞迴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級、2級、3級分類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;}//通過ID擷取所有的下級分類public function sel_son($id){$this->db->where("parent_id=$id");return $this->db->get(self::$cate)->result_array();}//渲染展示首頁模板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架構無限級分類+遞迴的實現代碼,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.