ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
控制器代码 \Application\Admin\Controller\ArticleController.class.php ~~~ <?php namespace Admin\Controller; use Admin\Controller\AuthController; use Think\Auth; class ArticleController extends AuthController { public function _initialize() { parent::_initialize(); $this->db = D('Article'); $this->ArticleCateModel = D('ArticleCate'); } // 文章列表 public function index() { $count = $this->db->where($map)->count(); $page = new \Think\Page($count , C('DEFAULT_PAGE_LIMIT')); $list = $this->db->where($map)->order('id desc')->limit($page->firstRow.','.$page->listRows)->select(); foreach ($list as $k=>$v){ $list[$k]['catename'] = $this->ArticleCateModel->where( array('id'=>$v['cid']))->getField('name'); } $this->list = $list; $this->page = $page->show(); $this->catelist = $this->ArticleCateModel->select(); $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->ArticleCateModel->select(); $this->display(); } } //删除文章 public function del(){ $id = I('id'); $res = $this->db->delete($id); if ($res) { $this->ajaxReturn(['code'=>1,'msg'=>'删除成功']); }else{ $this->ajaxReturn(['code'=>0,'msg'=>'删除失败']); } } } ~~~ 模型 \Application\Admin\Model\ArticleModel.class.php ~~~ <?php namespace Admin\Model; use Think\Model; class ArticleModel extends Model { /** * 自动验证 * @var array */ protected $_validate = array( array('cid', 'require', '请选择分类'), array('title', 'require', 'title不能为空'), array('content', 'require', 'content不能为空'), array('sort', 'require', 'sort不能为空'), array('introductions', 'require', '简介不能为空'), ); /** * 自动完成 * @var array */ protected $_auto = array( array('create_time', NOW_TIME, 1), array('update_time', NOW_TIME, 3), array('content','htmlspecialchars_decode',3,'function'), // 对name字段在新增和编辑的时候回调getName方法 ); /** * 更新数据 * @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(); } } } ~~~ 列表页面 \Application\Admin\View\Article\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('Article/update')}" > <input type="button" value="添加文章"> </a> </div> <div class="btn_box floatL"> <form id="form" action="__SELF__" method="post"> &nbsp;&nbsp;&nbsp;&nbsp; <span class="sttl">文章标题:</span> <input name="title" type="text" size="16" style="background-color: #fff;color:#cdcdcd;"> <input type="submit" value="查询" > </form> </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> <th>操作</th> </tr> <volist name="list" id="vo"> <tr> <td>{$vo.id}</td> <td>{$vo.title}</td> <td>{$vo.catename}</td> <td>{$vo.sort}</td> <td>{$vo.update_time|date="Y-m-d H:i:s",###}</td> <td><if condition="$vo['status'] eq 1"><span class="f_cB">启用</span><else />禁止</if></td> <td> <a href="{:U('Article/update',array('id'=>$vo[id]))}">编辑</a> <a class="del" link="{:U('Article/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\Article\update.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('Article/index')}" 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"> <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="txtbox floatL"> <textarea rows="5" cols="80" name="introductions">{$info.introductions}</textarea> </div><span class="f_cB">(必填)</span></td> </tr> <tr> <th>文章内容</th> <td><div class="txtbox floatL"> <script id="content" name="content" type="text/plain" style="width:800px;height:300px;">{$info.content}</script> </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>&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 --> <js href="__PUBLIC__/Admin/js/jquery-2.0.2.js" /> <js href="__PUBLIC__/static/ueditor/ueditor.config.js" /> <js href="__PUBLIC__/static/ueditor/ueditor.all.min.js" /> <js href="__PUBLIC__/static/ueditor/lang/zh-cn/zh-cn.js" /> <script> $(function(){ var UEDITOR_HOME_URL = "http://img.user.com/Public/static/ueditor/"; var ue = UE.getEditor('content',{ serverUrl: UEDITOR_HOME_URL + "/php/controller.php" }); $("#form").submit(function(e){ var res = $(this).serialize(); var url = "{:U('Article/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('Article/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` ( `id` int(11) NOT NULL AUTO_INCREMENT, `cid` tinyint(4) NOT NULL COMMENT '分类ID', `title` varchar(50) NOT NULL COMMENT '文章标题', `introductions` varchar(250) NOT NULL COMMENT '摘要', `content` text NOT NULL COMMENT '文章内容', `sort` int(11) NOT NULL DEFAULT '50' COMMENT '排序', `create_time` int(11) NOT NULL COMMENT '创建时间', `update_time` int(11) NOT NULL COMMENT '更新时间', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COMMENT='// 文章'; ~~~