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.