thinkphp infinite classification implementation method

大家讲道理
Release: 2023-03-05 21:40:02
Original
4897 people have browsed it

This article explains the method of implementing infinite classification under the thinkphp framework. Infinite classification is generally used in the classification menu of websites. It is a very common data structure and function. It is also very easy to implement this method in thinkphp. Okay, let's learn how to use it next.

The principle of infinite 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. When outputting, recursion is generally used.

Let’s first create a new data table with the following structure:

thinkphp infinite classification implementation method

Controller: CateAction .class.php

<?php
 class CateAction extends Action{
 function index(){
 $cate=M(&#39;Cate&#39;);
 $list=$cate->field("id,name,pid,path,concat(path,&#39;-&#39;,id) as bpath")->order(&#39;bpath&#39;)->select();
 foreach($list as $key=>$value){
 $list[$key][&#39;count&#39;]=count(explode(&#39;-&#39;,$value[&#39;bpath&#39;]));
 }
 $this->assign(&#39;alist&#39;,$list);
 $this->display();
 }//添加栏目
 function add(){
 $cate=new CateModel();if($vo=$cate->create()){
 if($cate->add()){
 $this->success(&#39;添加栏目成功&#39;);
 }else{
 $this->error(&#39;添加栏目失败&#39;);
 }
 }else{
 $this->error($cate->getError());
 }
 }}
 ?>
Copy after login

Model: CateModel.class.php

<?php
 class CateModel extends Model{//对应数据库中的表xp_cate
 protected $_auto=array(
 array(&#39;path&#39;,&#39;tclm&#39;,3,&#39;callback&#39;),
 );function tclm(){
 $pid=isset($_POST[&#39;pid&#39;])?(int)$_POST[&#39;pid&#39;]:0;
 echo ($pid);
 if($pid==0){
 $data=0;
 }else{
 $list=$this->where("id=$pid")->find();
 $data=$list[&#39;path&#39;].&#39;-&#39;.$list[&#39;id&#39;];//子类的path为父类的path加上父类的id
 }
 return $data;
 }
 }
 ?>
Copy after login

Template :index.html

<form action="!-URL-!/add" method="post">
 请选择父级栏目:<select name="pid" size="20">
 <option value="0">根栏目</option>
 <volist name="alist" id="vo">
 <option value="{$vo[&#39;id&#39;]}">
   {:str_repeat(" ",$vo[&#39;count&#39;]-2)}
 {$vo[&#39;name&#39;]}
 </option>
 </volist>
 </select><br />
 新的栏目名称:<input type="text" name="name" /><br />
 <input type="submit" value="添加栏目" />
 </form>
Copy after login

The display results are as follows:

thinkphp infinite classification implementation method


##Note:

The infinite-level classification implemented in this article uses the ThinkPhP framework. That is to say, the MVC architecture is adopted, in which the controller, template and model layers are clearly written. It is easy to understand for those who can use TP. If there are students who do not understand the TP framework, you can first understand how to use the framework. Looking back at our writing style.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template