This article explains how to implement unlimited classification under the thinkphp framework. the unlimited classification is generally used in the classification menu of a website. it is a common data structure and function. For more information, see. this article explains how to implement unlimited classification under the thinkphp framework. the unlimited classification is generally used in the classification menu of a website. it is a very common data structure and function, it is also easy to implement this method in thinkphp. next we will learn how to use it.
The principle of unlimited classification is to add a field (such as Sid) for differentiation. The top-level classification Sid is 0, the second-level classification Sid is the ID of the upper-level classification, and so on. Recursion is generally used for output.
First, create a data table. the table structure is as follows:
Controller: CateAction. class. php
Field ("id, name, pid, path, concat (path, '-', id) as bpath")-> order ('bpath')-> select (); foreach ($ list as $ key => $ value) {$ list [$ key] ['count'] = count (explode ('-', $ value ['bpath']);} $ this-> assign ('alist', $ list); $ this-> display ();} // add function add () {$ cate = new CateModel (); if ($ vo = $ cate-> create ()) {if ($ cate-> add () {$ this-> success ('column added successfully ');} else {$ this-> error ('add topic failed');} else {$ this-> error ($ cate-> getE Rror () ;}}}?>
Model: CateModel. class. php
Where ("id = $ pid")-> find (); $ data = $ list ['path']. '-'. $ list ['id']; // The path of the subclass is the path of the parent class plus the id of the parent class.} return $ data ;}}?>
Template: index.html
The result is as follows:
The infinite classification implemented in this article uses the ThinkPhP framework. That is to say, the MVC architecture is used. the controller, template, and model layers are clearly written. it is easy to understand the controller, template, and model layer. if you do not understand the TP framework, you can first understand how to use the framework, and look back at our writing method.