Thinkphp unlimited classification implementation program code

Source: Internet
Author: User

Database structure

Id pid username

1 0 www.111cn.net
2 1 mb.111cn.net
3 2 down.111cn.net

From the above, we can see that id = 1 is the final root level, and the other is the sub-level. Let's take a look at how to read Infinite categories in thinkphp.

First of all, this is thinkphp Infinitus classification, so we will write it according to the thinkphp structure. For example, if the display method of your template is index (), write it in the index () method:

$ Message = M ('message'); // comment
// It is necessary to explain the where condition. Of course, I am the code in the project. The comment must display the comment of the current article. This condition is used to query the comment of the current article.

The code is as follows: Copy code

Public $ listall = array ();

$ Listmessage = $ message-> where ('article _ id = '. $ list ['artid']. '')-> order ('Id desc')-> select (); // your table name is message

Foreach ($ listmessage as $ v ){
If ($ v ['pid '] = 0 ){
Echo $ v ['username']. "<br/> ";
$ This-> listall [] = $ v;
$ This-> wuxian_f ($ v ['id'], $ nb );
}
}

 

The following is the called Infinitus classification method:

The code is as follows: Copy code

// Infinitus classification test
Protected function wuxian_f ($ fid, $ nb ){
$ Message = M ('message ');
$ Hf = $ message-> where ('pid = "'. $ fid.'" ')-> select ();
If ($ hf ){
Foreach ($ hf as $ c ){
$ Nb = $ nb ."----";
Echo $ nb. $ c ['username']. "<br/> ";
$ This-> listall [] = $ c;
$ Hc = $ message-> where ('pid = "'. $ c ['id'].'" ')-> find ();
If ($ hc ){
$ Nb = $ nb ."----";
Echo $ nb. $ hc ['username']. "<br/> ";
$ This-> listall [] = $ hc;
$ This-> wuxian_f ($ hc ['id'], $ nb );
}
}
}
}

If you want to traverse through the foreground, assign $ this-> assign ('mess ', $ this-> listall); listall to The view page, then, the front-end page will loop through mess.

Analysis is actually a recursive search and then output. The method is very simple.

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.