abstract:這一節(jié)主要練習(xí)了欄目分類增、刪、改、查操作,代碼如下:<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/4/29 * Time: 17:29 */ name
這一節(jié)主要練習(xí)了欄目分類增、刪、改、查操作,代碼如下:
<?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(); //獲取數(shù)據(jù) $data['time']=time(); //添加時間 $data['username']=Session::get('username'); //獲取用戶 //實例化 $sort = new SortModel(); //數(shù)據(jù)入庫 驗證返回提示 if($sort->save($data)){ return ['res'=>1,'msg'=>'添加成功']; }else{ return ['res'=>0,'msg'=>'添加失敗']; } } public function edit() { //修改前獲取ID $sortId = Request::param('id'); //通過id查詢數(shù)據(jù) $sort = SortModel::get($sortId); //賦值給模版 $this->view->sort = $sort; return $this->fetch(); } public function DoEdit() { //修改操作 $data = Request::param(); //實例化模型 $sort = new SortModel(); $info = $sort->save([ //返回對應(yīng)值 'title'=>$data['title'], //準(zhǔn)備數(shù)據(jù) '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最基礎(chǔ)的語句就是增刪查改,后臺管理就是練習(xí)mysql的操作。繼續(xù)加油。