How to implement infinite grading with recursion under the TP5 Framework (code example)

Source: Internet
Author: User
This article brings the content is about the TP5 framework under the recursive implementation of infinite Classification (code example), there is a certain reference value, the need for friends can refer to, I hope to help you.

I use the TP5 framework, because the project with unlimited classification, so I create a extend inside a catetree package and then create a class catetee.php, you can directly reference in the controller can be

<?phpnamespace catetree;class catetree {public Function Catetree ($cateRes) {return $this->sort ($cateR    ES);        The Public function sort ($cateRes, $pid =0, $level =0) {static $arr =array ();                foreach ($cateRes as $k = + $v) {if ($v [' pid ']== $pid) {$v [' level ']= $level;                $arr []= $v;            $this->sort ($cateRes, $v [' id '], $level + 1);    }} return $arr;        }//Get sub-column ID public function childrenids ($cateid, $obj) {$data = $obj->field (' Id,pid ')->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 [' pid ']== $cateid) {$arr []= $v [' id '];            $this->_childrenids ($data, $v [' id ']);   }} return $arr; }//Processing column sort public functioN Catesort ($data, $obj) {foreach ($data as $k + $v) {$obj->update ([' id ' = + $k, ' sort ' = ' + $v]); }}//Process bulk Delete public function Pdel ($cateids) {foreach ($cateids as $k = = $v) {$children                idsarr[]= $this->childrenids ($v);            $childrenidsarr []= (int) $v;            } $_childrenidsarr=array (); foreach ($childrenidsarr as $k = + $v) {if (Is_array ($v)) {foreach ($v as $k 1 + = $v                    1) {$_childrenidsarr[]= $v 1;                }}else{$_childrenidsarr[]= $v;            }} $_childrenidsarr=array_unique ($_childrenidsarr);       $this::d Estroy ($_childrenidsarr); }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.