123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- namespace app\index\controller;
- use think\Controller;
- use think\Request;
- class Base extends Controller//公共控制器
- {
- protected function _initialize()
- {
- $column = $this->tops();//栏目信息
- $ta = $this->tags();//标签信息
- $this->assign([
- 'column'=>$column,//栏目信息
- 'ta'=>$ta,//标签信息
- ]);
-
- }
- protected function tops(){//头部栏目数据
- $co = db('column')->order('id asc')->select();
- return $co;
- }
- protected function tags(){//热门标签
- $tag = db('article')->field('id,keywords')->where('keywords','not null')->select();
- //***解决重复关键词(keywords)只显示其中的一个
- $qc=[];
- foreach ($tag as $ka){
- $key = explode(",",$ka['keywords']);
- foreach ($key as $k){
- $qc[]=$k;
- }
- }
- $tags = array_unique($qc);
- //_______________________________________________end
- return $tags;
- }
- public function search(){//热门标签搜索
- $keywords=input('keywords');
- if($keywords){
- $map['title']=['like','%'.$keywords.'%'];
- $ke['keywords']=['like','%'.$keywords.'%'];
- $searchres=db('article')->where($map)->whereOr($ke)->select();
- $this->assign(array(
- 'searchres'=>$searchres,
- 'keywords'=>$keywords,
- ));
- }else{
- echo "<script>alert('非法请求!');location.href='".url('index/index')."'</script>";
- }
- return view();
- }
- public function searchtop(){//头部关键词搜索
- if(request()->isPost()) {
- $keywords = input('keywords');
- if($keywords){
- $map['title']=['like','%'.$keywords.'%'];
- $ke['keywords']=['like','%'.$keywords.'%'];
- $search=db('article')->where($map)->whereOr($ke)->select();
- if($search){
- $this->assign(array(
- 'search'=>$search,
- 'keywords'=>$keywords,
- ));
- }else{
- $this->assign(array(
- 'search'=>null,
- 'keywords'=>'没有搜索到关键词!',
- ));
- }
- }
- }else{
- echo "<script>alert('非法请求');location.href='".url('index/index')."'</script>";
- }
- return view();
- }
-
-
- }
|