This paper explains the method of realizing infinite class classification under the thinkphp Framework, which is commonly used in the classification menu of the website, is a very common data structure and function, and it is very easy to implement this method in thinkphp, then we will learn how to use it.
The principle of infinite classification is to add a field (such as SID) as a distinction, the top-level classification SID is 0, the two-level classification SID is the ID of the previous classification, and so on. Recursion is generally used when outputting.
Let's start by creating a new data table with the following structure:
Controller: CateAction.class.php
<?php class 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 Column function Add () {$cate =new catemodel (); if ($vo = $cate->create ()) {if ($cate->add ()) {$this->success (' Add column success '); }else{$this->error (' Add column failed ');}} else{$this->error ($cate->geterror ());}}} ?>
Model: CateModel.class.php
<?php class Catemodel extends model{//corresponds to the table in the database Xp_cate protected $_auto=array (Array (' path ', ' TCLM ', 3, ' callback '),); 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 parent class and the parent class's ID} return $data; }}?>
Template: index.html
<form action= "!-url-!/add" method= "POST" > Please select parent column: <select name= "pid" size= "0" > <option value= Root column </option> <volist name= "alist" id= "Vo" > <option value= "{$vo [' id ']}" > {: Str_repeat ("", $vo [' Count ']-2}} {$vo [' name ']} </option> </volist> </select><br/> New column name: <input type= "Text" Name= "name"/><br/> <input type= "Submit" value= "add column"/> </form>
The results appear as follows:
Note:
The Infinite class classification implemented in this paper uses the thinkphp framework. That is, the MVC architecture, where the controller, template and model layer is written very clearly, for the use of TP is easy to understand, if there is no understanding of the TP framework of the students, you can first understand the framework of the use of the method, in the back look at our wording.