Home > Backend Development > PHP Tutorial > About the implementation of infinite classification and recursion in CI framework

About the implementation of infinite classification and recursion in CI framework

不言
Release: 2023-04-01 07:38:02
Original
1867 people have browsed it

CodeIgniter is a lightweight but powerful PHP framework. Based on the MVC design pattern, it provides a rich set of class libraries that are easy to learn, efficient and practical. The following is an introduction to the implementation code of infinite classification recursion of the CI framework. Interested friends can refer to it

What is CI?

CodeIgniter is a lightweight but The powerful PHP framework, based on the MVC design pattern, provides a rich set of class libraries that are easy to learn, efficient and practical.

Let’s take a look at the implementation code of CI framework infinite classification recursion. The specific code is as follows:

//无级分类+递归
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(&#39;    &#39;,$val[&#39;level&#39;])?><?php echo $val[&#39;cat_name&#39;]?></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[&#39;cat_id&#39;]);
foreach($tmp as $kk=>$vv){
$tmp2 = $this->sel_son($vv[&#39;cat_id&#39;]);
$tmp[$kk][&#39;childs&#39;] = $tmp2;
}
$arr[$k][&#39;child&#39;] = $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[&#39;type&#39;] = $this->Home_model->sel_child($p_id);
$brr[&#39;list&#39;] = $this->db->get(&#39;goods&#39;)->result_array();
$this->load->view(&#39;Home/list.html&#39;,$brr);
}
<?php foreach($type as $v){?>
<li id="cat_1" class="">
<h3><a href=""><?php echo $v[&#39;cat_name&#39;]?></a></h3>
<?php foreach($v[&#39;child&#39;] as $vv){?>
<dl class="clearfix">
<dt><a href=""><?php echo $vv[&#39;cat_name&#39;]?></a></dt>
<?php foreach($vv[&#39;childs&#39;] as $vvv){?>
<a href=""><?php echo $vvv[&#39;cat_name&#39;]?></a>
<?php }?>
</dl>
<?php }?>
</li>
<?php }?>
Copy after login

The above is the entire content of this article , I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to use the CodeIgniter framework to implement image uploading

About the CodeIgniter framework verification code class library file Analysis of usage

The above is the detailed content of About the implementation of infinite classification and recursion in CI framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template