新闻中心对新闻数据的查询以及赋值

原创 2018-12-09 15:24:16 224
摘要:/*********************   index.php 前端控制器**************************/<?phpnamespace app\index\controller;use think\Controller;use app\admin\model\SlideModel;use app\admin\model\ProductModel

/*********************   index.php 前端控制器**************************/

<?php
namespace app\index\controller;
use think\Controller;
use app\admin\model\SlideModel;
use app\admin\model\ProductModel;
use app\admin\model\NewsModel;
use app\admin\model\SystemModel;
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',1)->select()->toArray();
       $this->view->products = $products;

       //查询新上花魁
       $NewProduct = $product->where('sort',2)->limit(1)->select()->toArray();
       $this->view->NewProduct = $NewProduct;

       //查询最新资讯
       $new = new NewsModel();
       $news = $new->limit(4)->order('id', 'desc')->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')->paginate(3);
       $this->view->produts = $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();
       //查询数据按照id的顺序查询并且每页四条数据
       $news = $new->order('id','desc')->paginate(4);
       //给模板继续赋值
       $this->view->news = $news;

       //热门新闻
       $hotNew = $new->limit(1)->select()->toArray();
       $this->view->hotNews = $hotNew;
       //最新发布
       $newNews = $new->limit(6)->select()->toArray();
       $this->view->newNews = $newNews;

       //渲染首页模板
       return $this->fetch();
   }

   public function ConNew()
   {
       $newId = Request::param('id');
       //通过id查询对应的新闻详情
       $new = NewsModel::get($newId);
       $this->view->new = $new;

       //热门新闻
       $hotNew = $new->limit(1)->select()->toArray();
       $this->view->hotNews = $hotNew;
       //最新发布
       $newNews = $new->limit(6)->select()->toArray();
       $this->view->newNews = $newNews;
       //渲染首页模板
       return $this->fetch();
   }

}
/******************  news.html 新闻中心页面*******************/

{include file="public/style" /}
<body>
<div id="page">
   {include file="public/head" /}
   <div id="body">
       <div class="header">
           <div>
               <h1 style="color: #6E6E6E; font-family: YouYuan; font-weight: bold">新闻中心</h1>
               <div class="article">
                   <ul>
                       {volist name="news" id="new"}
                       <li>
                           <a href="{:url('ConNew')}?id={$new.id}"><img src="{:GetNewsPic($new.id)}" style="border-radius:10px; box-shadow: 0 0 0px #6E6E6E;   width: 100%;height: 100%;" alt=""></a>
                           <h1>{$new.title}</h1>
                           <span style="color: #0C0C0C">{$new.time|date="Y-m-d"}</span>
                           <p style="color: #0C0C0C">{$new.desc}</p>
                           <a href="{:url('ConNew')}?id={$new.id}" class="more">阅读更多</a>
                       </li>
                      {/volist}
                   </ul>
               </div>
               <div class="sidebar">
                   <ul>
                       {volist name="hotNews" id="hotNew"}
                       <li>
                           <h1>热门新闻</h1>
                           <a href="{:url('ConNew')}?id={$hotNew.id}"><img src="{:GetNewsPic($hotNew.id)}" style="border-radius:10px; box-shadow: 0 0 5px #6E6E6E;   width: 340px;height: 188px;" alt=""></a>
                           <h2>{$hotNew.title}</h2>
                           <span>{$hotNew.desc}</span>
                       </li>
                       {/volist}
                       <li>
                           <h1>最新发布</h1>
                           <ul>
                               {volist name="newNews" id="newNew"}
                               <li>
                                   <a href="{:url('ConNew')}?id={$newNew.id}"><img src="{:GetNewsPic($newNew.id)}" style="border-radius:10px; box-shadow: 0 0 2px #6E6E6E;   width: 60px;height: 55px;" alt="{$newNew.title}"></a>
                                   <h2 style="font-size: 15px;">{$newNew.title}</h2>
                                   <span>{$newNew.time|date="Y-m-d"}</span>
                               </li>
                               {/volist}
                           </ul>
                       </li>
                   </ul>
               </div>
           </div>

       </div>
       <div class="page">
           <div>
               {$news|raw}
           </div>
       </div>
   </div>
   {include file="public/floor" /}
</div>
</body>
</html>

/******************** con_new.html 新闻详情 **************/

{include file="public/style" /}
<body>
<div id="page">
   {include file="public/head" /}
   <div id="body">
       <div class="header">
           <div>
               <h1 style="color: #6E6E6E; font-family: YouYuan; font-weight: bold">新闻详细</h1>
               <div class="article">
                   <img src="{:GetNewsPic($new.id)}" style="border-radius:10px; box-shadow: 0 0 5px #6E6E6E;   width: 560px;height: 240px;" alt="">
                   <div style="border-radius:10px; box-shadow: 0 0 5px #6E6E6E; background: #BDBDBD; padding: 20px;">
                       <h1 style=" text-align:center; color: #000;">{$new.title}</h1>
                       <span style="color: #0C0C0C">{$new.time|date="Y-m-d"}</span>
                       <p>{:htmlspecialchars_decode($new.content)}</p></div>
               </div>
               <div class="sidebar">
                   <ul>
                       {volist name="hotNews" id="hotNew"}
                       <li>
                           <h1>热门新闻</h1>
                           <a href="{:url('ConNew')}?id={$hotNew.id}"><img src="{:GetNewsPic($hotNew.id)}" style="border-radius:10px; box-shadow: 0 0 5px #6E6E6E;   width: 340px;height: 188px;" alt=""></a>
                           <h2>{$hotNew.title}</h2>
                           <span>{$hotNew.desc}</span>
                       </li>
                       {/volist}
                       <li>
                           <h1>最新发布</h1>
                           <ul>
                               {volist name="newNews" id="newNew"}
                               <li>
                                   <a href="{:url('ConNew')}?id={$newNew.id}"><img src="{:GetNewsPic($newNew.id)}" style="border-radius:10px; box-shadow: 0 0 2px #6E6E6E;   width: 60px;height: 55px;" alt=""></a>
                                   <h2 style="font-size: 15px;">{$newNew.title}</h2>
                                   <span>{$newNew.time|date="Y-m-d"}</span>
                               </li>
                               {/volist}
                           </ul>
                       </li>
                   </ul>
               </div>
           </div>
       </div>
   </div>
   {include file="public/floor" /}
</div>
</body>
</html>


总结:本套课程学习到了,用tp5.1框架制作企业站的整个流程,从后台的制作以及前台模板的渲染,对后台权限有了新的认识,利用mvc结构,对数据的增、删、改、查更加熟悉,思路更加清晰。

批改老师:韦小宝批改时间:2018-12-09 15:39:40
老师总结:写的很不错!代码很完整!下次记得要给代码加上高亮哦!

发布手记

热门词条