abstrait:新聞縮略圖模塊上傳和刪除測(cè)試練習(xí)代碼<?php /** * Created by PhpStorm. * User: Administrator * Date: 2019/4/26 * Time: 18:46 */ namespace&nbs
新聞縮略圖模塊上傳和刪除測(cè)試練習(xí)代碼
<?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() { //查找新聞所有的數(shù)據(jù) $news =NewsModel::all(); //將數(shù)據(jù)賦值給模板 $this->view->news=$news; //渲染縮略圖添加界面 return $this->fetch(); } public function upload(){ //獲取上傳圖片的信息 $file =Request::file('file'); if($info = $file->validate(['ext'=>'jpg,jpeg']) //驗(yàn)證后綴 ->move('upload')){ //移動(dòng)到指定目錄 $fileName = '/upload/'.$info->getSaveName(); //拼接并獲取圖片路徑 return json([1,'上傳成功','data'=>$fileName]); //返回信息 }else{ return $file->getError(); //返回失敗信息 } } public function DoAdd() { //提交圖片數(shù)據(jù) $data = Request::param(); $data['time'] = time(); $data['username'] =Session::get('username'); //實(shí)例化模型 $newsPic = new NewsPicModel(); //存儲(chǔ)數(shù)據(jù) if($newsPic->save($data)){ return ['res'=>1,'msg'=>'發(fā)布成功']; }else{ return ['res'=>0,'msg'=>'發(fā)布失敗']; } } public function del() { $picId=Request::param('id'); $newPic = new NewsPicModel(); if($newPic->destroy($picId)){ return ['res'=>1,'msg'=>'刪除成功']; } } }
圖片對(duì)應(yīng)的新聞標(biāo)題查詢,需要在公共文件中創(chuàng)建方法進(jìn)行關(guān)聯(lián)ID查詢
<?php // 應(yīng)用公共文件 //公共文件下通過id查詢新聞標(biāo)題 function GetTitle($news_id){ //通過id查詢新聞標(biāo)題 return Db::connect('yejuzhi') //連接數(shù)據(jù)庫 ->table('news') //查詢表 ->where('id',$news_id) //查詢條件 ->value('title'); //返回值title }
Professeur correcteur:查無此人Temps de correction:2019-04-27 17:27:58
Résumé du professeur:完成的不錯(cuò),cms后臺(tái)就是對(duì)數(shù)據(jù)庫數(shù)據(jù)進(jìn)行操作。繼續(xù)加油