Implementation of thinkphp Infinitus classification

Source: Internet
Author: User
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.

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.