[thinkPHP5项目实战_26]前台文章关键词搜索

电玩女神 2022-06-04 23:06 295阅读 0赞

每篇文章下面展示有关键词,点击关键词可以查询该栏目下的所有具有该关键词的文章;

点击关键词,需要将该关键词当做参数传值tahs控制器进行联表查询;

在index模块下新建控制器Tags.php:

  1. <?php
  2. namespace app\index\controller;
  3. class Tags extends Basic
  4. {
  5. public function index()
  6. {
  7. $tags=input('tags');//获取传过来的关键词
  8. $map=['a.keywords' => ['like','%'.$tags.'%']];//拼接查询关键词的语句
  9. //联表查询article表中所有与用户搜索相同的关键词的该栏目下的文章,,2篇文章为一页;按照文章的artid进行排序
  10. $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')->where($map)->paginate(2);
  11. $this->assign('artres',$artres);
  12. return $this->fetch('tags');//返回模板tags.html
  13. }
  14. }

查询到的文章需要展示到对应的模板上;

新建视图文件Tags/tags.html:

与Lists/lists.html代码一样,拷贝即可

  1. <body>
  2. {include file="Public/header" /}
  3. <div class="container">
  4. <div class="row">
  5. <div class="col-sm-8 blog-main">
  6. {volist name="artres" id="vo"}
  7. <div class="post multi-post cate2 auth1">
  8. <h4 class="post-date">{$vo.time|date="Y年m月d日",###}</h4>
  9. <h2 class="post-title"><a href="{:url('Article/index',array('artid'=>$vo['artid']))}">{$vo.title}</a></h2>
  10. <div class="post-body"><p>{$vo.desc}</p>
  11. {if condition="$vo['pic'] neq ''"}
  12. <p style="text-indent: 0em;"><a title="2016年就一个字:戒!" target="_self" href="{:url('Article/index',array('artid'=>$vo['artid']))}"><img src="__PUBLIC__{$vo.pic}" title="你我网" alt="你我网"/></a></p>
  13. {/if}
  14. </div>
  15. <h5 class="post-tags">Tags: <span class="tags">
  16. <?php
  17. $arr=explode(',', $vo['keywords']);
  18. foreach ($arr as $k => $v) {
  19. echo "<a href='http://localhost/test/tp5/Public/index.php/index/Tags/index/tags/$v'>$v</a>";
  20. echo ' ';
  21. }
  22. ?>
  23. </span></h5>
  24. <h6 class="post-footer">
  25. 发布:渣渣 | 分类:{$vo.catename} | 评论:666 | 浏览:{$vo.click} | <a href="{:url('Article/index',array('artid'=>$vo['artid']))}">阅读全文 > </a>
  26. </h6>
  27. </div>
  28. {/volist}
  29. <div class="post pagebar">{$artres->render()}</div>
  30. </div>
  31. {include file="Public/sidebar"}
  32. </div>
  33. </div>
  34. {include file="Public/footer" /}
  35. </body>

效果:

Center

发表评论

表情:
评论列表 (有 0 条评论,295人围观)

还没有评论,来说两句吧...

相关阅读