Home > Backend Development > PHP Tutorial > ThinkPHP自动填充实现无限级分类的方法_PHP

ThinkPHP自动填充实现无限级分类的方法_PHP

WBOY
Release: 2016-05-31 19:30:24
Original
760 people have browsed it

ThinkPHP

本文实例展示了ThinkPHP自动填充实现无限级分类的方法,是ThinkPHP常用功能之一,非常具有实用价值。现将完整实例分享给大家,供大家参考。具体实现步骤如下:

表aoli_cate如下图所示:

一、action部分:

aoli/Home/Lib/Action/CataAction.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('添加栏目失败');  
      }
      //dump($vo);  
    }else{
      $this->error($cate->getError());  
    }
  }
  
}
&#63;>

Copy after login

二、模型部分:

aoli/Home/Lib/Model/CataModel.class.php文件如下:

<&#63;php
class CateModel extends Model{//对应数据库中的表aoli_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;>

Copy after login

三、view视图部分

aoli/Home/Tpl/default/Cate/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['id']}">
            <php>
              for($i=0;$i<$vo['count'];$i++){
                echo ' ';  
              }
            </php>
            {$vo['name']}
          </option>
         </volist>
        </select><br />
 新的栏目名称:<input type="text" name="name" /><br />
 <input type="submit" value="添加栏目" />
</form>

Copy after login

相信本文所述实例对大家ThinkPHP程序设计有一定的参考价值。

Related labels:
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