How to use recursion to achieve infinite grading under the TP5 framework (code example)

不言
Release: 2023-04-03 20:32:01
Original
2494 people have browsed it

The content of this article is about how to use recursion to achieve infinite grading (code examples) under the TP5 framework. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

I use the tp5 framework. Since many parts of the project use infinite grading, I directly create a catetree package in extend and then create a class Catetee.php, which can be directly referenced in the controller. That’s it

<?php
namespace catetree;
class Catetree {
    
    public function catetree($cateRes){
        return $this->sort($cateRes);
    }
    public function sort($cateRes,$pid=0,$level=0){
        static $arr=array();
        foreach ($cateRes as $k => $v) {
            if($v[&#39;pid&#39;]==$pid){
                $v[&#39;level&#39;]=$level;
                $arr[]=$v;
                $this->sort($cateRes,$v[&#39;id&#39;],$level+1);
            }
        }
        return $arr;
    }
    //获取子栏目id
   public function childrenids($cateid,$obj){
        $data=$obj->field(&#39;id,pid&#39;)->select();
        return $this->_childrenids($data,$cateid,TRUE);
   }
   private function _childrenids($data,$cateid,$clear=FALSE){
        static $arr=array();
        if($clear){
          $arr=array();
        }
        foreach ($data as $k => $v) {
            if($v[&#39;pid&#39;]==$cateid){
                $arr[]=$v[&#39;id&#39;];
                $this->_childrenids($data,$v[&#39;id&#39;]);
            }
        }
        return $arr;
   }
   //处理栏目排序
   public function cateSort($data,$obj){
    foreach ($data as $k => $v) {
        $obj->update([&#39;id&#39;=>$k,&#39;sort&#39;=>$v]);
    }
   }
   //处理批量删除
   public function pdel($cateids){
            foreach ($cateids as $k => $v) {
                $childrenidsarr[]=$this->childrenids($v);
                $childrenidsarr[]=(int)$v;
            }  
            $_childrenidsarr=array();
            foreach ($childrenidsarr as $k => $v) {
                if(is_array($v)){
                    foreach ($v as $k1 => $v1) {
                       $_childrenidsarr[]=$v1;
                    }
                }else{
                    $_childrenidsarr[]=$v;
                }
            }
            $_childrenidsarr=array_unique($_childrenidsarr);
            $this::destroy($_childrenidsarr);
    
   }
}
Copy after login

Related recommendations:

PHP implements stepless recursive classification (ThinkPHP framework), stepless thinkphp

ThinkPHP implements recursive stepless classification with less code, thinkphp recursive

The above is the detailed content of How to use recursion to achieve infinite grading under the TP5 framework (code example). 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!