abstract:产品图片模块开发和新闻图片模块基本一样,练习代码如下:<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/4/29 * Time: 10:32 */ name
产品图片模块开发和新闻图片模块基本一样,练习代码如下:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/4/29 * Time: 10:32 */ namespace app\admin\controller; use app\admin\controller\Common; use app\admin\model\ProductModel; use app\admin\model\ProductPicModel; use think\facade\Request; use think\facade\Session; class ProductPic extends Common { public function index() { //实例化模型 $proPic = new ProductPicModel(); $productPic = $proPic->order('id','desc')->paginate(2); $this->view->productPic = $productPic; //渲染产品缩略图列表 return $this->fetch(); } public function add() { //查询数据 $product = ProductModel::all(); //给模版赋值 $this->view->product = $product; //渲染添加界面 return $this->fetch(); } public function upload() { //获取图片 $file = Request::file('file'); //验证 if($info = $file->validate(['ext'=>'jpg,jpeg,png']) ->move('upload')){ //移动 //拼接路径 $fileName = '/upload/'.$info->getSaveName(); //返回图片上传成功的信息 return json([1,'上传成功','data'=>$fileName]); }else { //返回上传图片失败的信息 return $file->getError(); } } public function DoAdd() { //添加操作 $data = Request::param(); $data['time'] = time(); $data['username'] = Session::get('username'); //实例化 $proPic = new ProductPicModel(); if($proPic->save($data)){ return ['res'=>1,'msg'=>'发布成功']; }else{ return ['res'=>0,'msg'=>'发布失败']; } } public function del() { //获取删除产品的id $productId = Request::param('id'); //实例化 $productPic = new ProductPicModel(); //删除并验证 if($productPic->destroy($productId)){ //删除操作 return ['res'=>1,'msg'=>'删除成功']; //返回成功信息 }else{ return ['res'=>0,'msg'=>'删除失败']; //返回失败信息 } } }
Correcting teacher:查无此人Correction time:2019-04-30 09:11:39
Teacher's summary:完成的不错。php操作mysql最基础的语句就是增删查改,后台管理就是练习mysql的操作。继续加油。