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

Jun 14, 2018 pm 02:04 PM
ci recursion

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:

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

//无级分类+递归

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
Recursive implementation of C++ functions: Is there a limit to recursion depth? Recursive implementation of C++ functions: Is there a limit to recursion depth? Apr 23, 2024 am 09:30 AM

The recursion depth of C++ functions is limited, and exceeding this limit will result in a stack overflow error. The limit value varies between systems and compilers, but is usually between 1,000 and 10,000. Solutions include: 1. Tail recursion optimization; 2. Tail call; 3. Iterative implementation.

Do C++ lambda expressions support recursion? Do C++ lambda expressions support recursion? Apr 17, 2024 pm 09:06 PM

Yes, C++ Lambda expressions can support recursion by using std::function: Use std::function to capture a reference to a Lambda expression. With a captured reference, a Lambda expression can call itself recursively.

Count the number of occurrences of a substring recursively in Java Count the number of occurrences of a substring recursively in Java Sep 17, 2023 pm 07:49 PM

Given two strings str_1 and str_2. The goal is to count the number of occurrences of substring str2 in string str1 using a recursive procedure. A recursive function is a function that calls itself within its definition. If str1 is "Iknowthatyouknowthatiknow" and str2 is "know" the number of occurrences is -3. Let us understand through examples. For example, input str1="TPisTPareTPamTP", str2="TP"; output Countofoccurrencesofasubstringrecursi

Recursive program to find minimum and maximum elements of array in C++ Recursive program to find minimum and maximum elements of array in C++ Aug 31, 2023 pm 07:37 PM

We take the integer array Arr[] as input. The goal is to find the largest and smallest elements in an array using a recursive method. Since we are using recursion, we will iterate through the entire array until we reach length = 1 and then return A[0], which forms the base case. Otherwise, the current element is compared to the current minimum or maximum value and its value is updated recursively for subsequent elements. Let’s look at various input and output scenarios for this −Input −Arr={12,67,99,76,32}; Output −Maximum value in the array: 99 Explanation &mi

Recursive implementation of C++ functions: Comparative analysis of recursive and non-recursive algorithms? Recursive implementation of C++ functions: Comparative analysis of recursive and non-recursive algorithms? Apr 22, 2024 pm 03:18 PM

The recursive algorithm solves structured problems through function self-calling. The advantage is that it is simple and easy to understand, but the disadvantage is that it is less efficient and may cause stack overflow. The non-recursive algorithm avoids recursion by explicitly managing the stack data structure. The advantage is that it is more efficient and avoids the stack. Overflow, the disadvantage is that the code may be more complex. The choice of recursive or non-recursive depends on the problem and the specific constraints of the implementation.

How to use Vue form processing to implement recursive nesting of forms How to use Vue form processing to implement recursive nesting of forms Aug 11, 2023 pm 04:57 PM

How to use Vue form processing to implement recursive nesting of forms Introduction: As the complexity of front-end data processing and form processing continues to increase, we need a flexible way to handle complex forms. As a popular JavaScript framework, Vue provides us with many powerful tools and features to handle recursive nesting of forms. This article will introduce how to use Vue to handle such complex forms, and attach code examples. 1. Recursive nesting of forms In some scenarios, we may need to deal with recursive nesting.

C++ Recursion Advanced: Understanding Tail Recursion Optimization and Its Application C++ Recursion Advanced: Understanding Tail Recursion Optimization and Its Application Apr 30, 2024 am 10:45 AM

Tail recursion optimization (TRO) improves the efficiency of certain recursive calls. It converts tail-recursive calls into jump instructions and saves the context state in registers instead of on the stack, thereby eliminating extra calls and return operations to the stack and improving algorithm efficiency. Using TRO, we can optimize tail recursive functions (such as factorial calculations). By replacing the tail recursive call with a goto statement, the compiler will convert the goto jump into TRO and optimize the execution of the recursive algorithm.

Detailed explanation of C++ function recursion: application of recursion in string processing Detailed explanation of C++ function recursion: application of recursion in string processing Apr 30, 2024 am 10:30 AM

A recursive function is a technique that calls itself repeatedly to solve a problem in string processing. It requires a termination condition to prevent infinite recursion. Recursion is widely used in operations such as string reversal and palindrome checking.

See all articles