先来对数据表进行分析
Field | Type | Comment | |
catId | int(11) NOT NULL | ||
parentId | int(11) NOT NULL | ||
isShow | tinyint(4) NOT NULL | ||
catName | varchar(20) NOT NULL | ||
priceSection | text NULL | ||
catSort | int(11) NOT NULL | ||
catFlag | tinyint(4) NOT NULL | ||
isFloor | tinyint(4) NOT NULL |
parentId:表示当前分类的父 id,他是实现无限级分类的关键
无限级分类,主要从两个方面进行考虑的。
1, 数据库的设计,表中的 parentId 字段。
2, 在程序的层面上,来完成,使用递归。
说白了也就是 parentId 就是他的老爸的 ID 一直到 parentId 为 0 这就到了顶级分类
添加分类,首先写入 Model 层
//新增 public function added() { $goodscategoryInfo = $this->create(); if( !empty( $goodscategoryInfo ) ) { $res = $this->add( $goodscategoryInfo ); if( $res !== false ) { return true; }else { return false; } }else { return false; } }
然后是写入 Controller 层
//添加 public function add() { $this->assign( 'goodscategoryList', D( 'goods_category' )->getlist() ); $this->assign( "act", U( "insert" ) ); $this->assign( "actInfo", "添加" ); $this->display( "goods_category_info" ); } public function insert() { $res = D( "goods_category" )->added(); if( $res !== false ) { $this->success( "添加成功" ); }else { $this->error( "添加失败" ); } }
最后在写入 View 层
<form class="form-horizontal" role="form" method="post" action="{$act}"> <div class="form-group"> <label for="firstname" class="col-sm-2 control-label">上级商品分类名称</label> <div class="col-sm-6"> <select name="parentId"> <option value="0">顶级分类</option> <volist name="goodscategoryList" id='vo'> <option value="{$vo['catid']}">{$vo.nbsp}{$vo['catname']}</option> </volist> </select> </div> </div>
好啦!完成添加功能,现在到了显示,首先写入 Model 层
//列表 public function getlist() { $cats = $this->select(); //获取树状的分类 return $this->getTree($cats); } //创建组织结构分类树(无限极分类) public function getTree( &$list, $parentId = 0, $level = 0, $nbsp = ' ' ) { static $tree = array(); foreach( $list as $v ) { if( $v['parentid'] == $parentId ) { //说明找到,保存 $v['nbsp'] = str_repeat( $nbsp, $level ); $tree[] = $v; //继续找 $this->getTree( $list, $v['catid'], $level + 1 ); } } return $tree; }
然后是写入 Controller 层
//获取列表 public function index() { $this->assign( 'goodscategoryList', D( 'goods_category' )->getlist() ); $this->display( 'goods_category_list' ); }最后在写入 View 层
<td>{$vo.nbsp}{$vo.catname}</td>最终目的要成为这样
商品分类名称 | 排序号 | 是否显示 | 操作 |
---|---|---|---|
时蔬水果、网上菜场 | 1 | 显示 | |
进口水果 | 0 | 显示 | |
橙柚 | 0 | 显示 | |
苹果 | 0 | 显示 | |
凤梨 | 1 | 显示 | |
火龙果 | 4 | 显示 | |
梨 | 6 | 显示 | |
芒果 | 8 | 显示 | |
蓝莓 | 12 | 显示 |
好啦!完成显示功能,现在到了更新,首先写入 Model 层
//更新 public function update() { $goodscategoryInfo = $this->create(); if( !empty( $goodscategoryInfo ) ) { $res = $this->save( $goodscategoryInfo ); if( $res !== false ) { return true; }else { return false; } }else { return false; } }然后是写入 Controller 层
//编辑 public function edit() { $this->assign( "goodscategoryList", D( 'goods_category' )->getlist() ); $this->assign( "goodscategoryInfo", D( 'goods_category' )->get() ); $this->assign( "act", U( "update" ) ); $this->assign( "actInfo", "更新" ); $this->display( "goods_category_info" ); } public function update() { $res = D( "goods_category" )->update(); if( $res !== false ) { $this->success( "更新成功" ); }else { $this->error( "更新失败" ); } }最后在写入 View 层
<form class="form-horizontal" role="form" method="post" action="{$act}"> <div class="form-group"> <label for="firstname" class="col-sm-2 control-label">上级商品分类名称</label> <div class="col-sm-6"> <select name="parentId"> <option value="0">顶级分类</option> <volist name="goodscategoryList" id='vo'> <option value="{$vo['catid']}">{$vo.nbsp}{$vo['catname']}</option> </volist> </select> </div> </div>
好啦!完成更新功能,现在到了删除,首先写入 Model 层
//删除 public function delete() { return $this->delete( I( "get.catid" ) ); }然后是写入 Controller 层
//删除 public function delete() { $res = M( 'goods_category' )->delete( I( "get.catid" ) ); if( $res !== false ) { $this->success( "删除成功" ); }else { $this->error( "删除失败" ); } }
注意:编辑的表单中一定要传 catId ,否则更新会失败
<input type="hidden" name="catId" value="<notempty name='goodscategoryInfo.catid'>{$goodscategoryInfo.catid}</notempty>">
此无限极分类功能存在两个问题
1,添加子类需要手动选择上级。
2,删除子类会同时删除子类下的子级。