Home  >  Article  >  Backend Development  >  Thinkphp unlimited classification code_php skills

Thinkphp unlimited classification code_php skills

WBOY
WBOYOriginal
2016-05-16 20:04:521002browse

This article will teach you how to write an infinite classification step by step. In fact, the most important thing to master a piece of knowledge is to master the logic of infinite classification, then it will be much easier to write.
First look at the database table: xp_cate

Controller: CateAction.class.php

<&#63;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();
}//添加栏目
function add(){
$cate=new CateModel();if($vo=$cate->create()){
if($cate->add()){
$this->success('添加栏目成功');
}else{
$this->error('添加栏目失败');
}
}else{
$this->error($cate->getError());
}
}}
&#63;>

Model: CateModel.class.php

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

Template: index.html

<form action="/Article/add" method="post">
请选择父级栏目:<select name="pid" size="20">
<option value="0">根栏目</option>
<volist name="alist" id="vo">
<option value="{$vo['id']}">
<for start="0" end="$vo['count']">
  
</for>
{$vo['name']}
</option>
</volist>
</select><br />
新的栏目名称:<input type="text" name="name" /><br />
<input type="submit" value="添加栏目" />
</form>

The results are displayed as follows:

Have you mastered the logic of infinite classification? The thinkphp infinite classification code shared above, I hope it will be helpful to your study.

Statement:
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