Realization method of thinkphp infinite pole classification

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

  • Related Article

    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.