Home > Article > Backend Development > Infinitus classification in php
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 |
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(); }
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); }
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 ; }
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:
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!