Home > PHP Framework > ThinkPHP > body text

Using ThinkPHP6 to implement recursive tree structure

王林
Release: 2023-06-20 14:48:10
Original
1132 people have browsed it

With the development of the Internet, tree-structured displays have appeared in various websites and applications, such as classification directories, personnel organization structures, permission management, etc. In these application scenarios, the recursive tree structure has become one of the very important and practical models.

ThinkPHP6 is a PHP development framework based on the MVC model. It has a rich extension library and excellent performance, and is widely recognized and used by developers. The implementation of recursive tree structures in ThinkPHP6 has become more convenient.

Below, we will introduce how to use recursive functions to build a tree structure in ThinkPHP6.

1. Define the database structure

Before implementing the recursive tree structure, you first need to know how to store data in the database so that the application can process it. In this example, we will create a "category" table and store information such as category name, category ID, parent ID, etc. in the category table.

The classification table structure is as follows:

id int(11) primary key
name varchar(50) classification name
parent_id int(11) parent classification ID

2. Implement the recursive function

Next, we need to implement a recursive function to query all child nodes starting from the root node. In ThinkPHP6, you can use the select method combined with the $where parameter to query specified columns, for example:

Db::name('category table')->where('parent_id',$id) ->select();

In this example, $id is the parameter passed to the recursive function, indicating the ID of the current node. The recursive function will recursively query all child nodes of the node based on the ID.

The following is the implementation of the recursive function:

function getChildren($id){
    //查询该节点下的所有子节点
    $children=Db::name('分类表')->where('parent_id',$id)->select();
    //如果没有子节点,返回空数组
    if(empty($children)){
        return $children;
    }
    //递归查询子节点的子节点,并将结果合并到$children数组中
    foreach($children as $k=>$v){
        $children[$k]['children']=$this->getChildren($v['id']);
    }
    return $children;
} 
Copy after login

In this function, we first query all child nodes under the node and save the results in the $children array. If the node has no child nodes, an empty array is returned directly.

Next, we use a foreach loop to traverse each child node in the $children array and call the recursive function to query all child nodes of the child node. Merge the results into the $children array, eventually returning the entire $children array.

3. Output tree structure

When the recursive function obtains the information of the node and all its sub-nodes, we need to output them as a tree structure. This can be achieved by looping through the array returned by the recursive function and outputting the corresponding indentation symbols based on the depth of each node.

The following is the code to output the tree structure:

function outputTree($arr,$deep=0){
    //定义缩进符号
    $symbol='|--';
    $html='';
    
    foreach($arr as $v){
        //根据节点深度输出缩进符号
        $html.=str_repeat('       ',$deep).$symbol.$v['name'].'<br/>';
        //如果有子节点,继续遍历
        if(!empty($v['children'])){
            $html.=$this->outputTree($v['children'],$deep+1);
        }
    }
    
    return $html;
}
Copy after login

In this function, we first define the indentation symbol, and then recursively traverse each node in the array. Outputs the corresponding number of indentation symbols based on the depth of the current node. If a node has child nodes, continue recursively traversing all child nodes of the node.

Finally, the code to output the entire tree structure is as follows:

$id=0;
$arr=$this->getChildren($id);
$html=$this->outputTree($arr);
echo $html;
Copy after login

In this code, $id represents the ID of the root node. We first call the recursive function to obtain the information of all child nodes, Then call the function that outputs the tree structure to output the entire tree structure to the HTML page.

4. Summary

By using ThinkPHP6’s rich extension library and recursive functions, we can easily build a recursive tree structure, making the application easier to manage and use. I hope this article can help you with your development work when building a tree structure, allowing you to complete the task more efficiently.

The above is the detailed content of Using ThinkPHP6 to implement recursive tree structure. 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