92 lines
2.9 KiB
PHP
Executable File
92 lines
2.9 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Home\Controller;
|
|
|
|
use Gy_Library\DBCont;
|
|
use Gy_Library\HomeController;
|
|
|
|
class SearchController extends HomeController
|
|
{
|
|
|
|
public function index()
|
|
{
|
|
$this->meta_title = '搜索';
|
|
//分享标题
|
|
$this->share_title = '搜索';
|
|
$word = I('get.word');
|
|
$this->word = $word;
|
|
if (isset($word) && $word ==='') {
|
|
$map['_string'] = '1=0';
|
|
$this->show_keywords=1;
|
|
$keyword_res = D('Keywords')->where(['status'=>DBCont::YES_BOOL_STATUS])->order('total_time desc,sort asc,id asc')->select();
|
|
$this->keyword =$keyword_res;
|
|
}
|
|
|
|
if (isset($word) && $word !=='') {
|
|
// $condition = array('like', '%' . htmlentities($word) . '%');
|
|
$condition = array('like', '%' . $word . '%');
|
|
$map['status'] = DBCont::NORMAL_STATUS;
|
|
$map['title|content|author'] = array(
|
|
$condition,
|
|
$condition,
|
|
$condition,
|
|
'_multi'=>true
|
|
);
|
|
$this->search_title = $word;
|
|
|
|
}
|
|
|
|
$model = D('News');
|
|
//页码
|
|
$page = !empty(I('page'))?I('page'):1;
|
|
$per_page = C('HOME_PER_PAGE_NUM');
|
|
$count = $model->getListForCount($map);
|
|
if($per_page === false){
|
|
$para = array($count);
|
|
}
|
|
else{
|
|
$para = array($count, $per_page);
|
|
}
|
|
$search_res = $model->where($map)->page($page, $per_page)->order('id desc')->select();
|
|
$this ->count =$count;
|
|
foreach ($search_res as &$item){
|
|
$cate_id = explode(',',$item['cate_id']);
|
|
$item['cate_id_desc'] = $cate_id;
|
|
$item['url'] = '/Home/news/detail?cate_id='.$item['cate_id_desc'][0].'&id='.$item['id'];
|
|
$item['select'] = 'much';
|
|
}
|
|
$class = new \ReflectionClass('\Gy_Library\GyPage');
|
|
$page_object = $class->newInstanceArgs($para);
|
|
$pagination = $page_object->show();
|
|
$this->assign('pagination', $pagination);
|
|
$this->assign('res', $search_res);
|
|
$this->display();
|
|
}
|
|
|
|
public function sub_key_word($str,$key,$len=100,$enc='utf-8'){
|
|
$strlen = mb_strlen($str,$enc);
|
|
$keylen = mb_strlen($key,$enc);
|
|
$keypos = mb_strpos($str,$key,0,$enc);
|
|
$leftpos = $keypos - 1;
|
|
$rightpos = $keypos + $keylen;
|
|
if($keylen > $len){
|
|
return mb_substr($key,0,$len,$enc)."...";
|
|
}
|
|
$result = $key;
|
|
for($i = 0;$i<$len - $keylen;$i++){
|
|
if($leftpos >= 0){
|
|
$result = mb_substr($str,$leftpos--,$leftpos+$len,$enc);
|
|
}else{
|
|
$result .= mb_substr($str,$rightpos++,1,$enc);
|
|
}
|
|
}
|
|
if($leftpos >= 0){
|
|
$result = "...".$result;
|
|
}
|
|
if($rightpos < $strlen){
|
|
$result .= "...";
|
|
}
|
|
return $result;
|
|
}
|
|
}
|