首页 > 代码库 > thinkphp 无限极分类的数据库设计及效果测试

thinkphp 无限极分类的数据库设计及效果测试

控制器继承IndexAction.class.php

 <?php // 本类由系统自动生成,仅供测试用途 class IndexAction extends CateAction { } ?> 

控制器代码CateAction.class.php

<?phpclass 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());}}}?>

模块代码CateModel.class.php

<?phpclass CateModel extends Model{//对应数据库中的表xp_cateprotected $_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‘];//子类的path为父类的path加上父类的id}return $data;}}?>

模板代码index.html

<?phpclass CateModel extends Model{//对应数据库中的表xp_cateprotected $_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‘];//子类的path为父类的path加上父类的id}return $data;}}?>

thinkphp 无限极分类的数据库设计及效果测试