サマリー:網(wǎng)站開發(fā)前臺模塊控制器,可以實例化后臺的模型,在前臺查詢循環(huán)并顯示數(shù)據(jù)庫中的代碼即可,練習(xí)代碼如下:<?php namespace app\index\controller; use app\admin\model\NewsModel; use app\admin\model\ProductModel; use app\admin\model\Slid
網(wǎng)站開發(fā)前臺模塊控制器,可以實例化后臺的模型,在前臺查詢循環(huán)并顯示數(shù)據(jù)庫中的代碼即可,練習(xí)代碼如下:
<?php namespace app\index\controller; use app\admin\model\NewsModel; use app\admin\model\ProductModel; use app\admin\model\SlideModel; use app\admin\model\SystemModel; use think\Controller; use think\facade\Request; class Index extends Controller { //渲染網(wǎng)站首頁 public function index() { //實例化查詢輪播圖 $slide = new SlideModel(); //查詢縮略圖 $slides = $slide->select() //查詢 ->toArray(); //轉(zhuǎn)化成數(shù)組賦值 $this->view->slides=$slides; //實例化并查詢頭牌 $product = new ProductModel(); $products = $product->where('sort','2') //查詢條件 ->select() //查詢 ->toArray(); //轉(zhuǎn)換 $this->view->products = $products; //實例化并查詢花魁 $NewsProduct = $product->where('sort','4') ->select() ->toArray(); $this->view->NewsProduct = $NewsProduct; //實例化并查詢最新資訊 $new= new NewsModel(); $news = $new->limit(4)->select()->toArray(); $this->view->news = $news; return $this->fetch(); } //渲染關(guān)于我們 public function about() { $system = new SystemModel(); $systems = $system->select()->toArray(); $this->view->systems = $systems; return $this->fetch(); } //渲染產(chǎn)品展示 public function product() { $product = new ProductModel(); $products = $product->order('id','desc') //id排序 ->paginate(2); //2條分頁 $this->view->products = $products; return $this->fetch(); } //渲染產(chǎn)品顯示頁 public function ConPro() { //獲取傳遞過來的ID $ProID = Request::param('id'); $product = ProductModel::get($ProID); $this->view->product = $product; return $this->fetch(); } //渲染新聞?wù)故? public function news() { //實例化 $new = new NewsModel(); $news = $new->order('id','desc') ->paginate(4); $this->view->news = $news; //熱門新聞 $hotNew = $new->limit(1)->select()->toArray(); $this->view->hotNew = $hotNew; //最新發(fā)布 $newNew = $new->limit(5)->select()->toArray(); $this->view->newNew = $newNew; return $this->fetch(); } //新聞詳情 public function ConNew() { $newId = Request::get('id'); //通過ID查詢對應(yīng)的新聞詳細(xì) $new = NewsModel::get($newId); $this->view->new =$new; //熱門新聞 $hotNews = $new->limit(1)->select()->toArray(); $this->view->hotNews = $hotNews; //最新發(fā)布 $newNew = $new->limit(5)->select()->toArray(); $this->view->newNew = $newNew; return $this->fetch(); //渲染模板 return $this->fetch(); } }
添削の先生:查無此人添削時間:2019-05-08 09:38:05
先生のまとめ:完成的不錯。想學(xué)會框架,就要學(xué)框架的操作方法。繼續(xù)加油。