Base.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use think\Request;
  5. class Base extends Controller//公共控制器
  6. {
  7. protected function _initialize()
  8. {
  9. $column = $this->tops();//栏目信息
  10. $ta = $this->tags();//标签信息
  11. $this->assign([
  12. 'column'=>$column,//栏目信息
  13. 'ta'=>$ta,//标签信息
  14. ]);
  15. }
  16. protected function tops(){//头部栏目数据
  17. $co = db('column')->order('id asc')->select();
  18. return $co;
  19. }
  20. protected function tags(){//热门标签
  21. $tag = db('article')->field('id,keywords')->where('keywords','not null')->select();
  22. //***解决重复关键词(keywords)只显示其中的一个
  23. $qc=[];
  24. foreach ($tag as $ka){
  25. $key = explode(",",$ka['keywords']);
  26. foreach ($key as $k){
  27. $qc[]=$k;
  28. }
  29. }
  30. $tags = array_unique($qc);
  31. //_______________________________________________end
  32. return $tags;
  33. }
  34. public function search(){//热门标签搜索
  35. $keywords=input('keywords');
  36. if($keywords){
  37. $map['title']=['like','%'.$keywords.'%'];
  38. $ke['keywords']=['like','%'.$keywords.'%'];
  39. $searchres=db('article')->where($map)->whereOr($ke)->select();
  40. $this->assign(array(
  41. 'searchres'=>$searchres,
  42. 'keywords'=>$keywords,
  43. ));
  44. }else{
  45. echo "<script>alert('非法请求!');location.href='".url('index/index')."'</script>";
  46. }
  47. return view();
  48. }
  49. public function searchtop(){//头部关键词搜索
  50. if(request()->isPost()) {
  51. $keywords = input('keywords');
  52. if($keywords){
  53. $map['title']=['like','%'.$keywords.'%'];
  54. $ke['keywords']=['like','%'.$keywords.'%'];
  55. $search=db('article')->where($map)->whereOr($ke)->select();
  56. if($search){
  57. $this->assign(array(
  58. 'search'=>$search,
  59. 'keywords'=>$keywords,
  60. ));
  61. }else{
  62. $this->assign(array(
  63. 'search'=>null,
  64. 'keywords'=>'没有搜索到关键词!',
  65. ));
  66. }
  67. }
  68. }else{
  69. echo "<script>alert('非法请求');location.href='".url('index/index')."'</script>";
  70. }
  71. return view();
  72. }
  73. }