PHP unlimited recursive menu implementation process

Source: Internet
Author: User

In the past few days, I have seen that wireless classification is very popular in TP, and various codes are messy. I also released a new idea. Recursion is used, and the code is concise, about 10 lines.
My database structure is like this, and my column has only one table.

Cid fatherid name
Primary Key parent column ID

I have seen that many tutorials use path methods like 0-1-2-3 and lv = 3, which are roughly the same, but the implementation methods are different. My method uses recursion and is easy to understand.


Code directly:

// First make a variable in the class and store the related array: public $ tree = null; // then perform a test to output the public function test () result () {$ 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 ));}}


The structure in my database is that if a topic is a top-level topic without a parent topic), its fatherid is equal to null instead of 0, this is different from other cases, because I have some foreign keys that use the associated model, so 0 is not used as the top-level classification identifier.

The idea is roughly like this: Find all top-level columns and start recursion. In the recursion process, write the hierarchical relationship into the array sub-element, which is easier to understand and use.

Insert the row record as the last element of the array.

Check whether there are subcolumns in the current recursive column. If there are subcolumns, continue to send them to the Recursive Method for loop. Here, we do not judge whether the return value of the subcolumn is greater than 0, because if the return value is null, the FOR loop is not started. Pay attention to the FOR loop at the beginning of the recursive method.

The final result is a multi-dimensional array, with each row record as a sub-element, and the top-level column is followed by all its sub-columns.

If you want to sort the classification order, you can use order, which will not be affected.

Some may ask if this will cause efficiency problems. I personally think that the current classification is infinite, but it is just to express the concept of hierarchical relationships, there won't be a website that is really divided into thousands of columns, at least not in China. Generally, it is normal to have less than 200 columns. The code is worse than the performance. The recursion of 200 Columns cannot be implemented? If you think it is slow or has a serious impact on the performance, you can also consider using the cache. This will benefit from the usage within the cycle. After all, the column will not change once an hour, right?

Friends who like to discuss can join a group of 252799167

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

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.