Home  >  Article  >  Backend Development  >  CMSPRESS realizes unlimited classification (with code)

CMSPRESS realizes unlimited classification (with code)

php中世界最好的语言
php中世界最好的语言Original
2018-05-17 11:59:111508browse

This time I will bring you CMSPRESS to achieve unlimited classification (with code). What are the precautions for CMSPRESS to implement unlimited classification? . Here are actual cases, let’s take a look.

SuperInfinite Classification The core code is very simple and efficient and less than 10 lines
In addition, I want to find out the shortcomings of this classification and a more efficient and simple infinite classification method^_^

The core code is as follows

class Tool {
  static public $treeList = array(); //存放无限分类结果如果一页面有多个无限分类可以使用 Tool::$treeList = array(); 清空
  /**
   * 无限级分类
   * @access public 
   * @param Array $data   //数据库里获取的结果集 
   * @param Int $pid       
   * @param Int $count    //第几级分类
   * @return Array $treeList  
   */
  static public function tree(&$data,$pid = 0,$count = 1) {
    foreach ($data as $key => $value){
      if($value['Pid']==$pid){
        $value['Count'] = $count;
        self::$treeList []=$value;
        unset($data[$key]);
        self::tree($data,$value['Id'],$count+1);
      } 
    }
    return self::$treeList ;
  }  
}

$treeList[] Saving the sorting results basically means performing a sorting and saving, and then you can unset($data[$key]); because it can no longer be used
&$data uses parameter passing by address, combined with unset($data[$key]); to reduce the number of loops, which improves the efficiency several times.

But the Pid needs to be sorted by ASC, otherwise the display will be incomplete.

$value['Count'] = $count; For the current level, a tree structure will be generated by level in the template

The data structure before and after sorting is as follows

The fields required by the table are Id, Pid
Data structure before sorting
id pid
1 0
2 0
3 1
4 3

Sorted data structure

id pid count
1 0 1
3 1 2
4 3 3
2 0 1

//默认列表
  public function index() {  
    $menu = M('Menu');
    $list = $menu->order('Pid ASC,Morder DESC,Id ASC')->select();
    $this->assign('List',Tool::tree($list));  
         $this->display();
  }

Called in the controller

| -- <{$vo.Name}>

Using the template in the 3da5ee660f26a047cce0b9503e43e613 normal output, you can modify the fields that need to generate the tree structure to the above

Tested if For 3,000 items, it takes 0.5 seconds. For 1,000 items, it takes about 0.02 seconds. If it exceeds 3,000, the efficiency will be greatly reduced. The efficiency of about 2,000 is still relatively high without more detailed testing.

I believe you have mastered it after reading the case in this article. For more exciting methods, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the use of PHP delayed static binding

thinkPHP framework automatic filling principle and usage detailed explanation

The above is the detailed content of CMSPRESS realizes unlimited classification (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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