ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
控制器 \Application\Admin\Controller\ImageCateController.class.php ~~~ <?php namespace Admin\Controller; use Admin\Controller\AuthController; use Think\Auth; class ImageCateController extends AuthController { public function _initialize() { parent::_initialize(); $this->cate = D('ImageCate'); } public function cate() { $count = $this->cate->where($map)->count(); $page = new \Think\Page($count , C('DEFAULT_PAGE_LIMIT')); // $page = new \Think\Page($count , 5); $list = $this->cate->where($map)->order('id desc')->limit($page->firstRow.','.$page->listRows)->select(); $this->list = $list; $this->page = $page->show(); $this->display(); } public function cateUpdate() { if (IS_POST) { $data = I('post.'); $res = $this->cate->update($data); if ($res) { $this->ajaxReturn(['code'=>1,'msg'=>'更新成功']); }else{ $this->ajaxReturn(['code'=>0,'msg'=>$this->cate->getError()]); } }else{ $id = I('id'); if ($id) { $this->info = $this->cate->where(array('id'=>$id))->find(); } $this->cate = $this->cate->where($map)->order('id desc')->select(); $this->display(); } } } ~~~ 模型-图片分类 \Application\Admin\Model\ImageCateModel.class.php ~~~ <?php namespace Admin\Model; use Think\Model; class ImageCateModel extends Model { /* 数据库设置 */ protected $connection = 'DB_CONFIG2'; /** * 自动验证 * @var array */ protected $_validate = array( array('title', 'require', 'title 不能为空'), ); /** * 自动完成 * @var array */ protected $_auto = array( array('create_time', NOW_TIME, 1), array('update_time', NOW_TIME, 3), array('status',1), ); /** * 更新数据 * @DateTime 2018-05-04 * @return [type] [description] */ public function update($info){ $data = $this->create($info); // dump($this->getError());die; if(!$data){ //数据对象创建错误 return false; } /* 添加或更新数据 */ if(empty($data['id'])){ return $this->add(); }else{ return $this->save(); } } } ~~~ 数据表 ~~~ CREATE TABLE `lxl_image_cate` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(32) NOT NULL, `cid` int(11) NOT NULL, `create_time` int(11) NOT NULL, `update_time` int(11) NOT NULL, `status` tinyint(2) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ~~~ 图片分类 \Application\Admin\View\ImageCate\cate.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('Image/cateUpdate')}" > <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> </tr> <volist name="list" id="vo"> <tr> <td>{$vo.id}</td> <td>{$vo.title}</td> <td>{$vo.cid}</td> <td>{$vo.update_time|date="Y-m-d H:i:s",###}</td> <td> <a href="{:U('Image/cateUpdate',array('id'=>$vo[id]))}">编辑</a> </td> </tr> </volist> </table> </div> </div> <!-- /MainForm --> <!-- PageNum --> {$page} <!-- /PageNum --> </div> <!-- /Contents --> <include file="Public:footer" /> ~~~ 添加图片分类 \Application\Admin\View\ImageCate\cateUpdate.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('Image/cate')}" style="text-decoration: none;"> <input type="button" value="返回" > </a> </div> <!-- /btn_box --> </div> <!-- /TopMain --> <!-- 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="txtbox floatL"> <input type="text" name="title" id="title" size="100" value="{$info.title}" /> </div></td> </tr> <tr> <th>分类</th> <td> <div class="selectbox floatL mag_r20"> <select name="cid" id="cid"> <option value="0" <if condition="$info['cid'] eq 0">selected="selected"</if>>顶级分类</option> <volist name="cate" id="vo"> <option value="{$vo.id}" <if condition="$info['cid'] eq $vo['id']">selected="selected"</if>>{$vo.title}</option> </volist> </select> </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> $(function(){ $("#form").submit(function(e){ var res = $(this).serialize(); var url = "{:U('Image/cateUpdate')}"; $.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('Image/cate')}"; }); } else{ layer.alert(data.msg,{icon:5},function (index) { layer.close(index); window.location.reload(); }); } } }); return false; // 阻止表单跳转 }); }); </script> <include file="Public:footer" /> ~~~