Unlimited classification
# #What is infinite classification:
Infinite classification simply means that a class can be divided into a molecular class, and then a subclass can be divided into another subclass and so on indefinitely. It seems that Windows can create a new folder, and then create a folder within this folder.public function index(){ $cate=D('cate'); $cateres=$cate->catetree(); $this->assign('cateres',$cateres); $this->display(); }
First write the template in the index controller, and $cateres calls the catetree() method.
<?php namespace Admin\Model; use Think\Model; class CateModel extends Model { protected $_validate = array( array('catename','require','管理员名称不得为空!',1), ); public function catetree(){ $data=$this->order('id desc')->select(); return $this->resort($data); } public function resort($data,$pid=0,$level=0){ static $arr=array(); foreach ($data as $k => $v) { if ($v['pid']==$pid) { $v['level']=$level; $arr[]=$v; $this->resort($data,$v['id'],$level+1); } } return $arr; } }Let’s explain how to implement it in turn.
catetree method
Get data, $this means calling itself to query, and use id to sort. return returns the results of the query.
resort method $data: Obtained data
$pid=0: Top category starts with 0
$level=0 Classification level
First create an empty array to store the data, and use foreach to traverse it. If $v['pid']==$pid means the top-level id, then $v['level']=$level is also the top-level category. The queried data is stored in the array, and then continues to call itself $this- >resort($data,$v['id'],$level 1); Here $data is equal to $data $v['id'] is equal to $pid, $level 1 means $level increases by 1 each time. The results found are as follows,
Let’s continue to add a few more to see
This is the simple principle of our infinite classification. Next Section