《CMS技巧:phpcms v9最新全站搜索教程分享》要点:
本文介绍了CMS技巧:phpcms v9最新全站搜索教程分享,希望对您有用。如果有疑问,可以联系我们。
相关主题:PHPCMS教程
- /**
- * 关键词搜索
- */
- public function init() {
- //获取siteid
- $siteid = isset($_REQUEST['siteid']) && trim($_REQUEST['siteid']) ? intval($_REQUEST['siteid']) : 1;
- $SEO = seo($siteid);
- //搜索配置
- $search_setting = getcache('search');
- $setting = $search_setting[$siteid];
- $search_model = getcache('search_model_'.$siteid);
- $type_module = getcache('type_module_'.$siteid);
- if(isset($_GET['q'])) {
- if(trim($_GET['q'])=='') {
- header('Location: '.APP_PATH.'index.php?m=search');exit;
- }
- $typeid = emptyempty($_GET['typeid']) ? 0 : intval($_GET['typeid']);
- $time = emptyempty($_GET['time']) || !in_array($_GET['time'],array('all','day','month','year','week')) ? 'all' : trim($_GET['time']);
- $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
- $pagesize = 10;
- $q = safe_replace(trim($_GET['q']));
- $q = new_html_special_chars(strip_tags($q));
- $q = str_replace('%', '', $q); //过滤'%',用户全文搜索
- $search_q = $q; //搜索原内容
- //全站搜索
- $sql = "select ";
- $sql_time = $sql_tid = '';
- if($typeid) $sql_tid = ' AND typeid = '.$typeid;
- //按时间搜索
- if($time == 'day') {
- $search_time = SYS_TIME - 86400;
- $sql_time = ' AND adddate > '.$search_time;
- } elseif($time == 'week') {
- $search_time = SYS_TIME - 604800;
- $sql_time = ' AND adddate > '.$search_time;
- } elseif($time == 'month') {
- $search_time = SYS_TIME - 2592000;
- $sql_time = ' AND adddate > '.$search_time;
- } elseif($time == 'year') {
- $search_time = SYS_TIME - 31536000;
- $sql_time = ' AND adddate > '.$search_time;
- } else {
- $search_time = 0;
- $sql_time = '';
- }
- if($page==1 && !$setting['sphinxenable']) {
- //精确搜索
- $commend = $this->db->get_one("`siteid`= '$siteid' $sql_tid $sql_time AND `data` like '%$q%'");
- } else {
- $commend = '';
- }
- //如果开启sphinx
- if($setting['sphinxenable']) {
- $sphinx = pc_base::load_app_class('search_interface', '', 0);
- $sphinx = new search_interface();
- $offset = $pagesize*($page-1);
- $res = $sphinx->search($q, array($siteid), array($typeid), array($search_time, SYS_TIME), $offset, $pagesize, '@weight desc');
- $totalnums = $res['total'];
- //如果结果不为空 维易PHP培训网 http://www.bcty365.com
- if(!emptyempty($res['matches'])) {
- $result = $res['matches'];
- }
- } else {
- $sql = "`siteid`= '$siteid' $sql_tid $sql_time AND `data` like '%$q%'";
- $result = $this->db->listinfo($sql, 'searchid DESC', $page, 10);
- $das = $result;
- }
转载请注明本页网址:
http://www.vephp.com/jiaocheng/5859.html