ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
控制器代码 \Application\Admin\Controller\ArticleCateController.class.php ~~~ <?php namespace Admin\Controller; use Admin\Controller\AuthController; use Think\Auth; class ArticleCateController extends AuthController { public function _initialize() { parent::_initialize(); $this->db = D('ArticleCate'); } // 列表 public function index() { $list = $this->db->order('id desc')->select(); foreach ($list as $k=>$v){ if($v['cid'] == 0 ){ $list[$k]['catename'] ='顶级分类'; }else{ $list[$k]['catename'] = $this->db->where('id='.$v['cid'])->getField('name'); } } $this->list = $list; $this->display(); } /** * 编辑 */ public function update() { if (IS_POST) { $data = I('post.'); $res = $this->db->update($data); if ($res) { $this->ajaxReturn(['code'=>1,'msg'=>'更新成功']); }else{ $this->ajaxReturn(['code'=>0,'msg'=>$this->db->getError()]); } }else{ $id = I('id'); if ($id) { $this->info = $this->db->where(array('id'=>$id))->find(); } $this->list = $this->db->where(array('cid'=>0))->select(); $this->imgList = D('Image')->select(); $this->display(); } } /** * 删除分类 */ public function del(){ $id = I('id'); $res = $this->db->delete($id); if ($res) { $list = $this->db->where(array('pid'=>$id))->select(); if($list){ foreach ($list as $v){ $this->db->delete($v['id']); } } $this->ajaxReturn(['code'=>1,'msg'=>'删除成功']); }else{ $this->ajaxReturn(['code'=>0,'msg'=>'删除失败']); } } } ~~~ 模型 \Application\Admin\Model\ArticleCateModel.class.php ~~~ <?php namespace Admin\Model; use Think\Model; class ArticleCateModel extends Model { /* 数据库设置 */ protected $connection = 'DB_CONFIG2'; /** * 自动验证 * @var array */ protected $_validate = array( array('cid', 'require', '请选择分类'), array('name', 'require', '名称不能为空'), array('description', 'require', '描述不能为空'), array('sort', 'require', '排序不能为空'), ); /** * 自动完成 * @var array */ protected $_auto = array( array('create_time', NOW_TIME, 1), array('update_time', NOW_TIME, 3), ); /** * 更新数据 * @DateTime 2018-05-04 * @return [type] [description] */ public function update($info){ $data = $this->create($info); // dump($data);die; if(!$data){ //数据对象创建错误 return false; } /* 添加或更新数据 */ if(empty($data['id'])){ return $this->add(); }else{ return $this->save(); } } } ~~~ 列表页面 \Application\Admin\View\ArticleCate\index.html ~~~ <include file="Public:header" /> <include file="Public:top" /> <!-- Contents --> <div id="Contents"> <!-- TopMain --> <div id="TopMain"> <!-- btn_box --> <div class="btn_box floatL"> <a href="{:U('ArticleCate/update')}"> <input type="button" value="添加"> </a> </div> <!-- /btn_box --> </div> <!-- /TopMain --> <!-- MainForm --> <div id="MainForm"> <div class="form_boxA"> <h2>分类列表</h2> <table cellpadding="0" cellspacing="0"> <tr> <th>ID</th> <th>分类名称</th> <th>上级分类</th> <th>排序</th> <th>状态</th> <th>操作</th> </tr> <volist name="list" id="vo"> <tr> <td>{$vo.id}</td> <td>{$vo.name}</td> <td>{$vo.catename}</td> <td>{$vo.sort}</td> <td> <if condition="$vo['status'] eq 0"><span class="f_cA">禁用</span></if> <if condition="$vo['status'] eq 1"><span class="f_cC">启用</span></if> </td> <td> <a name="edit" href="{:U('ArticleCate/update',array('id'=>$vo[id]))}">编辑</a> <a class="del" link="{:U('ArticleCate/del',array('id'=>$vo['id']))}">删除</a> </td> </tr> </volist> </table> </div> </div> <!-- /MainForm --> <!-- PageNum --> {$page} <!-- /PageNum --> </div> <!-- /Contents --> <include file="Public:footer" /> ~~~ 编辑页面 \Application\Admin\View\ArticleCate\update.html ~~~ <include file="Public:header" /> <include file="Public:top" /> <!-- Contents --> <div id="Contents"> <div id="TopMain"> <div class="btn_box floatL"> <a href="javascript:history.back(-1);" > <input type="button" value="返回"> </a> </div> </div> <!-- MainForm --> <div id="MainForm" style="padding: 50px;"> <div class="form_boxC"> <form id="form" method="post"> <table cellpadding="0" cellspacing="0"> <tr> <th>分类</th> <td> <div class="selectbox floatL mag_r20"> <select name="cid"> <option value="0" <if condition="$info['cid'] eq 0">selected="selected"</if>>顶级分类</option> <volist name="list" id="vo"> <option value="{$vo.id}" <if condition="$info['cid'] eq $vo['id']">selected="selected"</if>>{$vo.name}</option> </volist> </select> </div> </td> </tr> <tr> <th>图片</th> <td> <div class="selectbox floatL mag_r20"> <select name="image_id"> <option value="0" <if condition="$info['image_id'] eq 0">selected="selected"</if>>请选择封面图</option> <volist name="imgList" id="vo"> <option value="{$vo.id}" <if condition="$info['image_id'] eq $vo['id']">selected="selected"</if>>{$vo.title}</option> </volist> </select> </div> </td> </tr> <tr> <th>分类名称</th> <td> <div class="txtbox floatL"> <input type="text" name="name" id="name" size="24" value="{$info.name}" /> </div> </td> </tr> <tr> <th>描述</th> <td> <div class="txtbox floatL"> <input type="text" name="description" id="description" size="100" value="{$info.description}" /> </div> </td> </tr> <tr> <th>排序</th> <td> <div class="txtbox floatL"> <input type="text" name="sort" id="sort" size="24" value="{$info.sort}" /> </div> </td> </tr> <tr> <th>状态</th> <td> <div> <input type="radio" name="status" value="1" <if condition="$info['status'] eq 1">checked="checked"</if> />开启&nbsp;&nbsp;&nbsp;&nbsp; <input type="radio" name="status" value="0" <if condition="$info['status'] eq 0">checked="checked"</if> />禁用 </div> </td> </tr> <tr> <th>&nbsp;</th> <td> <div class="btn_box" style="box-shadow: none;"> <input type="hidden" name="id" value="{$info.id}" /> <input type="submit" style="min-width:160px;" value="提交"> </div> </td> </tr> </table> </form> </div> </div> <!-- /MainForm --> </div> <!-- /Contents --> <script type="text/javascript"> $(function () { $("#form").submit(function(e){ var res = $(this).serialize(); var url = "{:U('ArticleCate/update')}"; $.ajax({ url: url, data: res, type: 'post', success:function(data){ if (data.code == 1) { layer.alert(data.msg,{icon:6},function (index) { layer.close(index); window.location.href = "{:U('ArticleCate/index')}"; }); } else{ layer.alert(data.msg,{icon:5},function (index) { layer.close(index); window.location.reload(); }); } } }); return false; // 阻止表单跳转 }); }); </script> <include file="Public:footer" /> ~~~ 数据库sql ~~~ CREATE TABLE `lxl_article_cate` ( `id` int(11) NOT NULL AUTO_INCREMENT, `cid` int(11) NOT NULL, `image_id` int(11) NOT NULL COMMENT '图片id', `name` varchar(50) NOT NULL, `description` varchar(255) NOT NULL, `sort` int(11) NOT NULL, `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态 是否显示', `create_time` int(11) NOT NULL, `update_time` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=13 DEFAULT CHARSET=utf8 COMMENT='文章分类'; ~~~