ThinkPHP学习札记(十一)自动填充一个无限极分类

WBOY
Release: 2016-06-13 12:55:09
Original
875 people have browsed it

ThinkPHP学习笔记(十一)自动填充一个无限极分类

创建数据库表:tb_cate:id,name,pid,path

action

<?php /**
 * ThinkPHP中的
 * 自动完成(无限极分类)
 * 		用户输入的字段并不是用户手动填写的
 *
 */
class AutoCateAction extends Action{
	public 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=D('Cate');
		if ($vo=$cate->create()) {
			dump($vo);
			if ($cate->add()){
				$this->success("注册成功");
			}else{
				$this->error($cate->getError());
			}
		}else{
			$this->error($cate->getError());
		}
	}
}
?>
Copy after login

html



<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>


Copy after login
父级栏目: 新栏目名:


cateModel

<?php class CateModel extends Model{
		protected $_auto=array(
			array('path','filldata',3,'callback'),
		);
		function filldata(){
			//因为在model当中,所以不用new,直接用this就可以了
			$pid=isset($_POST['pid'])?(int)$_POST['pid']:0;
			if ($pid==0)return 0;
			$pcate=$this->where('id='.$pid)->find();
				$path=$pcate['path'].'-'.$pcate['id'];
			return $path;
		}
	}
?>
Copy after login


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!