[thinkPHP5项目实战_29]前台首页和文章搜索功能完善

[thinkPHP5项目实战_29]前台首页和文章搜索功能完善

1.首页功能

首页展示出所有文章,因此需要单独处理,首先在后台将首页的栏目删除,并将首页栏目下的文章移到其他栏目或者删除;


在头部导航header.html单独对首页进行处理;


<div class="blog-masthead">
    <div class="container">
        <nav class="blog-nav">
        <a class="blog-nav-item" href="{:url('Index/index')}">首页</a>
            {volist name="navres" id="vo"}
            <a class="blog-nav-item" href="
                {if condition="$vo['type'] eq 0"}
                {:url('lists/index',array('cateid'=>$vo['ID']))}
                {else /}
                {:url('guest/index',array('cateid'=>$vo['ID']))}
                {/if}
                ">{$vo.catename}</a>
                {/volist}
        </nav>
    </div>
</div>



点击首页导航通过index.php控制器进行处理:

获取所有的文章并通过联表查询每篇文章对应的栏目名:


<?php
namespace app\index\controller;
class Index extends Basic
{
    public function index()
    {
    $artres= \think\Db::name('article')->alias('a')->join('cate c','c.ID = a.cateid','LEFT')->field('a.artid,a.title,a.pic,a.time,a.desc,a.click,a.keywords,c.catename')->order('a.artid desc')->paginate(2);
    $this->assign('artres',$artres);
        return $this->fetch();
    }
}

首页模板Index.html赋值,与文章列表模板赋值一样:

<body>
    <!-- 引入头部 -->
    {include file="Public/header" /}
    <div class="container">
      <div class="row">
        <div class="col-sm-8 blog-main">
            {volist name="artres" id="vo"}
            <div class="post multi-post cate2 auth1">
            <h4 class="post-date">{$vo.time|date="Y年m月d日",###}</h4>
            <h2 class="post-title"><a href="{:url('Article/index',array('artid'=>$vo['artid']))}">{$vo.title}</a></h2>
            <div class="post-body"><p>描述:{$vo.desc}</p>
            {if condition="$vo['pic'] neq ''"}  
            <p style="text-indent: 0em;"><a title="" target="_self" href="{:url('Article/index',array('artid'=>$vo['artid']))}"><img src="__PUBLIC__{$vo.pic}"/></a></p>
            {/if}
            </div>
            <h5 class="post-tags">关键词: <span class="tags">
            <?php
              $arr=explode(',', $vo['keywords']);
              foreach ($arr as $k => $v) {
                echo "<a href='http://localhost/test/tp5/Public/index.php/index/Tags/index/tags/$v'>$v</a>";
                echo ' ';
              }
            ?>
            </span></h5>
            <h6 class="post-footer">
              发布:渣渣 | 分类:{$vo.catename} | 评论:6 | 浏览:{$vo.click} | <a href="{:url('Article/index',array('artid'=>$vo['artid']))}">阅读全文 > </a>
            </h6>
            </div>
            {/volist}
          <div class="post pagebar">{$artres->render()}</div>
        </div>
          <div class="col-sm-3 col-sm-offset-1 blog-sidebar">
              <div class="sidebar-module sidebar-module-inset">
                  <h4>文章搜索:</h4>
                  <form method="post" action="{:url('Search/index')}">
                  <input type="text" name="keywords" id="edtSearch" size="12" /> 
                  <input type="submit" value="提交" id="btnPost" />
                  </form>
              </div>
          </div>
      </div>
    </div>
    {include file="public/footer" /}
</body>


2.文章搜索

文章搜索是通过搜索文章标题实现;


将搜索框放在每个页面的侧边栏上,将搜素的关键词在数据中对每篇文章题目进行比对,返回响应的文章信息列表;



搜索框:


             

<div class="sidebar-module sidebar-module-inset">
    <h4>文章搜索:</h4>
    <form method="post" action="{:url('Search/index')}">
    <input type="text" name="keywords" id="edtSearch" size="12" /> 
    <input type="submit" value="提交" id="btnPost" />
    </form>
</div>



对应的Search控制器方法为:

<?php
namespace app\index\controller;
class Search extends Basic
{
    public function index()
    {
    $keywords=input('keywords');//获取搜索关键词
    if($keywords){
    $map['title']  = ['like','%'.$keywords.'%'];//关键词模糊搜索语句
    $seares=\think\Db::name('article')->where($map)->order('artid desc')->paginate(2);//查询和分页
    $this->assign('seares',$seares);//模板赋值
    $this->assign('keywords',$keywords);
    }else{
    $this->assign('keywords','没有关键词');//没有关键词的情况处理
    $this->assign('seares',null);
 
    }
        return $this->fetch('search');
    }
}



Search.html进行模板赋值:

需要对没有文章的情况进行判断


<body>
{include file="Public/header" /}
    <div class="container">
      <div class="row">
        <div class="col-sm-8 blog-main">
          <div class="post single-post cate0 auth0">
          <h4 class="post-date"></h4>
          <h2 class="post-title">关键词:{$keywords}</h2>
          <div class="post-body"> 
 
          {if condition="$seares neq ' '"}
          {volist name="seares" id="vo"}
          <div>
            <br/><font size="+0.5"><a target="_blank" href="{:url('article/index',array('artid'=>$vo['artid']))}">题目:{$vo.title}</a></font>
            <br/>描述:{$vo.desc}
            <br/>
            <br/>
          </div>
          {/volist}
          {else /}
          没有搜索结果!
          {/if}
        </div>
        </div>
      </div>
    </div>
    {include file="Public/footer" /}
</body>


效果:


20171112145424895.gif


原文链接:https://blog.csdn.net/weixin_36185028/article/details/78511757


追梦猪网站致力于前端分享申明:本站内容搜集整理而来请勿商业用途,仅供交流所用,如若侵犯您的权益请邮件站长进行删除!

文明浏览请勿传播非法内容