This article mainly introduces Thinkphp's unlimited classification code, which is really important. What should I do if I don't write it? This article will show you how to create an unlimited category step by step. For more information, see
This article mainly introduces Thinkphp's unlimited classification code, which is really important. What should I do if I don't write it? This article will show you how to create an unlimited category step by step. For more information, see
In this article, we will teach you how to write an infinite classification. In fact, to master a knowledge, the most important thing is to master the logic of an infinite classification, so it is much easier to write it.
First look at the database table: xp_cate
Controller: CateAction. class. php
<? Phpclass CateAction extends Action {function index () {$ cate = M ('cate'); $ list = $ Cate-> 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 ('add Column Succeeded ');} else {$ this-> error ('failed to add a topic ');}} else {$ this-> error ($ cate-> getError () ;}}}?>
Model: CateModel. class. php
<? Phpclass CateModel extends Model {// The table xp_cateprotected $ _ auto = array ('path', 'tclm', 3, 'callback') in the database '),); function tclm () {$ pid = isset ($ _ POST ['pid '])? (Int) $ _ POST ['pid ']: 0; echo ($ pid); if ($ pid = 0) {$ data = 0 ;} else {$ list = $ this-> 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:
Have you mastered the infinite classification logic? The thinkphp infinite classification code shared above is helpful for your learning.