abstrakt:管理員增刪改查方法如下:<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/4/24 * Time: 17:14 */ namespace app\
管理員增刪改查方法如下:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/4/24 * Time: 17:14 */ namespace app\admin\controller; use app\admin\model\UserModel; use think\Controller; use think\facade\Request; class User extends Controller { public function index() { //實(shí)例化模型 $user = new UserModel(); $user =$user->order('id','desc') //降序排序 ->paginate(5); //每頁(yè)8條分頁(yè) $this->view->users =$user; //將數(shù)據(jù)賦值給模版 //渲染管理員界面 return $this->fetch(); } public function add() { //渲染管理員添加 return $this->fetch(); } public function DoAdd() { //獲取前臺(tái)提交過(guò)來(lái)的數(shù)據(jù) $data = Request::param(); //獲取當(dāng)前添加時(shí)間 $data['time'] = time(); //密碼加字母再加密 $data['password'] = md5($data['password'].'admin'); //獲取用戶(hù)名 $username = $data['username']; //使用用戶(hù)名查詢(xún)數(shù)據(jù)庫(kù)是否有對(duì)應(yīng)的數(shù)據(jù) $res = UserModel::where('username',$username)->find(); //判斷數(shù)據(jù)是否存在 if ($res == true){ return ['res' => 0,'msg'=>'用戶(hù)名已存在']; } //實(shí)例化 $user = new UserModel(); //驗(yàn)證數(shù)據(jù)是否存入數(shù)據(jù)庫(kù) if ($user->save($data)) { return ['res' => 1,'msg' => '添加成功']; }else{ return ['res' => 0,'msg' => '添加失敗']; } } public function edit() { //接收前臺(tái)傳遞過(guò)來(lái)的ID $userId = Request::param('id'); //通過(guò)id查詢(xún)對(duì)應(yīng)的數(shù)據(jù),并賦值給$user $user = UserModel::get($userId); //將查詢(xún)出來(lái)的數(shù)據(jù)給模板進(jìn)行賦值 $this->view->user = $user; //渲染編輯頁(yè)面 return $this->fetch(); } //編輯提交地址 public function DoEdit() { //獲取編輯修改提交過(guò)來(lái)的數(shù)據(jù) $data = Request::param(); //實(shí)例化模型 $user = new UserModel(); //對(duì)數(shù)據(jù)進(jìn)行更新 $res = $user->save([ 'username'=>$data['username'], 'phone'=>$data['phone'], 'email'=>$data['email'], 'password'=>md5($data['password'].'admin'), 'time'=>time(), ],['id'=>$data['id']]); //判斷修改數(shù)據(jù)情況 if($res){ return['res'=>1,'msg'=>'修改成功']; } } //數(shù)據(jù)刪除 public function del() { //獲取需要?jiǎng)h除的ID $userId = Request::param('id'); //實(shí)例化模型 $user = new UserModel(); //刪除并返回提示信息 if($user->destroy($userId)){ return ['res'=>1,'msg'=>'刪除成功']; } } }
Korrigierender Lehrer:查無(wú)此人Korrekturzeit:2019-04-26 13:33:49
Zusammenfassung des Lehrers:完成的不錯(cuò),后臺(tái)管理,最常用的就是增刪查改。繼續(xù)加油。