Implement infinite recursive menu in PHP

Source: Internet
Author: User

These days to see TP about wireless classification make a pretty fire, all kinds of code fly. I also released one of my ideas. Using recursion, the code is very concise, about 10 lines.
My database structure is like this, my column has only one table.

CID Fatherid Name
Primary key Parent Column identification name

I see that a lot of tutorials say that using a path method like 0-1-2-3, and the lv=3 hierarchy, is basically the same, but the way to achieve it is different. My method uses recursion and is better understood.

Directly on the code:

First, make a variable within the class, and store the related array: public
 $tree = null;
 Then a test method is used to output the result public
 function test () {
        $res = M (' channel ')->where (' Fatherid is null ')->select ();
        $this->createtree ($res);
        Dump ($this->tree);
 Here is the recursive method
 Private Function Createtree (array $data = null, $LV = 1) {
        for ($i = 0; $i < count ($data); $i + +) {
   $data [$i] [' lv '] = $LV;
            $this->tree[count ($this->tree)] = $data [$i];
            $res = M (' channel ')->where (' fatherid= '. $data [$i] [' CID '])->select ();
            $this->createtree ($res, ($LV + 1));
        }
 

Here's how the structure in my database is, if a column is a top-level column (without a parent), his fatherid equals null rather than 0, which is not the case with others, because I have some foreign keys that use the association model, so no 0 is used as the top-level category identifier.

The idea is roughly this, find all the top columns, then start recursion, and in the process of recursion, write the hierarchical relationship into the elements of the array, which is easier to understand and use.

The row record is then inserted as the last element of the array.

Then go to the query, the current recursive column, whether there are sub columns, if there is to continue to send recursive methods to cycle, there is no judge the return value of the column is greater than 0, because if the return of NULL, will not start the loop, note the beginning of recursive method for loop.

The final result is a multidimensional array, each row as a child element, and the top column is followed by all of his sub columns.

If you need to arrange the sorting order, you can use the orders and there is no effect.

Some people may ask, this does not produce the efficiency question, I personally think, now the classification says is the infinite level, actually is only in order to express to the level relations concept, does not have that website really to divide thousands of columns, at least China does not have. Generally speaking, 200 columns, belong to the normal, this code, is the performance of the poor, 200 columns of recursion are uncertain? If you feel slow or have a serious impact on performance, you can also consider the use of caching, such a use, cycle benefits, after all, the column will not change every hour?

A friend who likes to talk about it can add a group of 252799167

This article is from the "尛 lei" blog, please be sure to keep this source http://a3147972.blog.51cto.com/2366547/1216069

See more highlights of this column: http://www.bianceng.cn/webkf/PHP/

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.