Infinitus classification in php

不言
Release: 2023-03-28 19:20:01
Original
1869 people have browsed it

This article mainly introduces the Infinitus classification in php, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

1. First, the table corresponding to the Infinitus classification The structure

| cate_id    | cate_name|  parentid(默认0)|
| --------   | -----:   | :----: |
| 1          | 键盘     |   0    |
| 2          | 机械键盘  |   1    |
| 3          | cherry键盘|  2    |
Copy after login

It can be seen that the value of cherry's parentid is equal to its upper level cate_id, which is the core of Infinitus classification

2. The infinite classification function is to convert the database The data in is re-sorted
Code under the controller:

public function index(){
    $cate = D('category')->catetree();    
    $this->assign('cate',$cate);        
    $this->display();
    }
Copy after login

Here $cate calls the catetree() method, then below is the code in our corresponding Model

Model下的代码:public function catetree(){
    $data = $this->select();    
    return $this->resort($data);
   }
Copy after login

First query the corresponding category table All the data inside and return a new method

public function resort($data,$parentid=0,$level=0){
    static $ret = array();    foreach ($data as $key => $v) {        
    if ($v['parentid']==$parentid) {        
    $v['level']=$level;        
    $ret[]=$v;       
    $this->resort($data,$v['cate_id'],$level+1);
        }
    }    return $ret ;
}
Copy after login

first generates a static array, and then traverses the $data passed by the catetree() method, and determines The condition is to find the top piece of data. First save the top-level data into a static array, then call itself and pass the id of the top-level column as a parameter.
In this way$parentid=$v['cate_id']
The final wireless classification function is completed!

Related recommendations:

Physical rollback in php

Solution summary of password encryption in PHP

The above is the detailed content of Infinitus classification in php. 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