abstract:新闻缩略图模块上传和删除测试练习代码<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/4/26 * Time: 18:46 */ namespace&nbs
新闻缩略图模块上传和删除测试练习代码
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/4/26 * Time: 18:46 */ namespace app\admin\controller; use app\admin\controller\Common; use app\admin\model\NewsModel; use app\admin\model\NewsPicModel; use think\facade\Request; use think\facade\Session; class NewsPic extends Common { public function index() { //查询操作 $newsPic = new NewsPicModel(); $pics = $newsPic->order('id','desc') //查询用id降序排序 ->paginate(6); //分页 $this->view->pics =$pics; //渲染缩略图列表 return $this->fetch(); } public function add() { //查找新闻所有的数据 $news =NewsModel::all(); //将数据赋值给模板 $this->view->news=$news; //渲染缩略图添加界面 return $this->fetch(); } public function upload(){ //获取上传图片的信息 $file =Request::file('file'); if($info = $file->validate(['ext'=>'jpg,jpeg']) //验证后缀 ->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'); //实例化模型 $newsPic = new NewsPicModel(); //存储数据 if($newsPic->save($data)){ return ['res'=>1,'msg'=>'发布成功']; }else{ return ['res'=>0,'msg'=>'发布失败']; } } public function del() { $picId=Request::param('id'); $newPic = new NewsPicModel(); if($newPic->destroy($picId)){ return ['res'=>1,'msg'=>'删除成功']; } } }
图片对应的新闻标题查询,需要在公共文件中创建方法进行关联ID查询
<?php // 应用公共文件 //公共文件下通过id查询新闻标题 function GetTitle($news_id){ //通过id查询新闻标题 return Db::connect('yejuzhi') //连接数据库 ->table('news') //查询表 ->where('id',$news_id) //查询条件 ->value('title'); //返回值title }
Correcting teacher:查无此人Correction time:2019-04-27 17:27:58
Teacher's summary:完成的不错,cms后台就是对数据库数据进行操作。继续加油