abstract:网站开发前台模块控制器,可以实例化后台的模型,在前台查询循环并显示数据库中的代码即可,练习代码如下:<?php namespace app\index\controller; use app\admin\model\NewsModel; use app\admin\model\ProductModel; use app\admin\model\Slid
网站开发前台模块控制器,可以实例化后台的模型,在前台查询循环并显示数据库中的代码即可,练习代码如下:
<?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 { //渲染网站首页 public function index() { //实例化查询轮播图 $slide = new SlideModel(); //查询缩略图 $slides = $slide->select() //查询 ->toArray(); //转化成数组赋值 $this->view->slides=$slides; //实例化并查询头牌 $product = new ProductModel(); $products = $product->where('sort','2') //查询条件 ->select() //查询 ->toArray(); //转换 $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(); } //渲染关于我们 public function about() { $system = new SystemModel(); $systems = $system->select()->toArray(); $this->view->systems = $systems; return $this->fetch(); } //渲染产品展示 public function product() { $product = new ProductModel(); $products = $product->order('id','desc') //id排序 ->paginate(2); //2条分页 $this->view->products = $products; return $this->fetch(); } //渲染产品显示页 public function ConPro() { //获取传递过来的ID $ProID = Request::param('id'); $product = ProductModel::get($ProID); $this->view->product = $product; return $this->fetch(); } //渲染新闻展示 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; //最新发布 $newNew = $new->limit(5)->select()->toArray(); $this->view->newNew = $newNew; return $this->fetch(); } //新闻详情 public function ConNew() { $newId = Request::get('id'); //通过ID查询对应的新闻详细 $new = NewsModel::get($newId); $this->view->new =$new; //热门新闻 $hotNews = $new->limit(1)->select()->toArray(); $this->view->hotNews = $hotNews; //最新发布 $newNew = $new->limit(5)->select()->toArray(); $this->view->newNew = $newNew; return $this->fetch(); //渲染模板 return $this->fetch(); } }
Correcting teacher:查无此人Correction time:2019-05-08 09:38:05
Teacher's summary:完成的不错。想学会框架,就要学框架的操作方法。继续加油。