abstract:这一节主要练习了栏目分类增、删、改、查操作,代码如下:<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/4/29 * Time: 17:29 */ name
这一节主要练习了栏目分类增、删、改、查操作,代码如下:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/4/29 * Time: 17:29 */ namespace app\admin\controller; use app\admin\controller\Common; use app\admin\model\SortModel; use think\facade\Request; use think\facade\Session; use think\helper\Time; class Sort extends Common { public function index() { //实例化模型 $sort = new SortModel(); $sorts = $sort->order('id','desc')->paginate(2); $this->view->sorts=$sorts; //渲染分类列表首页 return $this->fetch(); } public function DoAdd() { //添加分类 $data = Request::param(); //获取数据 $data['time']=time(); //添加时间 $data['username']=Session::get('username'); //获取用户 //实例化 $sort = new SortModel(); //数据入库 验证返回提示 if($sort->save($data)){ return ['res'=>1,'msg'=>'添加成功']; }else{ return ['res'=>0,'msg'=>'添加失败']; } } public function edit() { //修改前获取ID $sortId = Request::param('id'); //通过id查询数据 $sort = SortModel::get($sortId); //赋值给模版 $this->view->sort = $sort; return $this->fetch(); } public function DoEdit() { //修改操作 $data = Request::param(); //实例化模型 $sort = new SortModel(); $info = $sort->save([ //返回对应值 'title'=>$data['title'], //准备数据 'time'=>\time(), 'username'=>Session::get('username'), ],['id'=>$data['id']]); //判断是否成功 if($info){ return ['res'=>1,'msg'=>'修改成功']; }else{ return ['res'=>0,'msg'=>'修改失败']; } } public function del() { //获取删除id $sortId = Request::param('id'); //实例化模型 $sort = new SortModel(); if ($sort->destroy($sortId)) { return ['res' => 1, 'msg' => '删除成功']; }else{ return ['res'=>0,'msg'=>'修改失败']; } } }
Correcting teacher:查无此人Correction time:2019-04-30 09:11:45
Teacher's summary:完成的不错。php操作mysql最基础的语句就是增删查改,后台管理就是练习mysql的操作。继续加油。