Welcome

首页 / 网页编程 / PHP / 使用ThinkPHP的自动完成实现无限级分类实例详解

一、实现效果



二、主要代码
1、模板


2、控制器

·index模块


·add模块



3、模型


三、代码
以便于各位看官复制测试
1、模板

<form action="__URL__/add" method="post">栏目<select name="fid" size=20><option value="0">栏目</option><volist name="list" id="vo"><option value="{$vo["id"]}">{$vo["blank"]}{$vo["name"]}</option></volist></select><br/>添加栏目:<input type="text" name="name"/><br/><input type="submit" value="添加"/> </form>
2、控制器
<?phpclass IndexAction extends Action{/**+----------------------------------------------------------* 默认操作+----------------------------------------------------------*/public function index(){$Column=new ColumnModel();$list=$Column->field("id,name,fid,sort,concat(sort,"-",id) nsort")->order("nsort asc")->select();foreach($list as $key=>$val){$layer=count(explode("-",$list[$key]["nsort"]));$list[$key]["blank"]="";for($i=0;$i<$layer;$i++){if($i==$layer-1){$list[$key]["blank"].="---|";}else{$list[$key]["blank"].="---";}}}$this->assign("list",$list);$this->display();}public function add(){$Column=new ColumnModel;$Column->create();if($Column->add()){$this->success("添加成功");}else{$this->error($Column->getError());}} } ?>
3、模型
<?phpclass ColumnModel extends Model{protected $_auto=array(array("name","trim",0,"function"),//过滤用户不小心输入的空白字符array("sort","createsort",0,"callback"),);/**自动完成sort字段*根据POST过来的fid来查询上级sort,以确定本级sort*/protected function createsort(){$fid=$_POST["fid"]?(int)$_POST["fid"]:0;//如果用户没有选择父栏目,则默认父栏目id为0if($fid!="0"){$list=$this->where("id=$fid")->find();$data=$list["sort"]."-".$fid;}else{$data="0";}return $data;}}?>
以上所述是小编给大家介绍的ThinkPHP的自动完成实现无限级分类实例详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!