PC站长网 欢迎您的到来

TOP

phpcms v9不限模型全站搜索(无法搜索解决方案)
2017-01-03 23:00:16 来源: 作者: 【 】 浏览:2589次 评论:0
phpcms v9搜索东西的时候 有时候你会发现无法搜索东西,这是怎么回事?

在使用官方默认的搜索工具是 你会发现 ,官方默认的是按照版块搜索的,所以你在搜索的时候 一定要有一个默认的模块,也就是$typeid,因为没有这个参数,系统不知道搜索那个模块。一般可以加一个默认的搜索版块,比如$typeid=1,意思就是搜索文章模块的内容。

但是这样做仍有一个很大的问题,如果我很多模块,并且我都想搜索怎么办?


简单修改一下v9默认的搜索功能,可以不按模型搜索全站内容
下面是被修改后的search模块中的index.php文件 
<?php 
defined('IN_PHPCMS') or exit('No permission resources.'); 
pc_base::load_sys_class('form','',0); 
pc_base::load_sys_class('format','',0); 
class index { 
    function __construct() { 
        $this->db = pc_base::load_model('search_model'); 
        $this->content_db = pc_base::load_model('content_model'); 
    } 
     
    /** 
     * 关键词搜索 
     */ 
    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_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']; 
                //如果结果不为空 
                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); 
            } 
           
            //如果结果不为空 
            if(!emptyempty($result) || !emptyempty($commend['id'])) { 
                foreach($result as $_v) { 
                    if($_v['typeid']) $sids[$_v['typeid']][] = $_v['id']; 
                } 
  
                if(!emptyempty($commend['id'])) { 
                    if($commend['typeid']) $sids[$commend['typeid']][] = $commend['id']; 
                } 
                $model_type_cache = getcache('type_model_'.$siteid,'search'); 
                $model_type_cache = array_flip($model_type_cache); 
                $data = array(); 
                foreach($sids as $_k=>$_val) { 
                    $tid = $_k; 
                    $ids = array_unique($_val); 
  
                    $where = to_sqls($ids, '', 'id'); 
                    //获取模型id 
                    $modelid = $model_type_cache[$tid]; 
  
                    //是否读取其他模块接口 
                    if($modelid) { 
                        $this->content_db->set_model($modelid); 
                     
                        /** 
                        * 如果表名为空,则为黄页模型 
                        */ 
                        if(emptyempty($this->content_db->model_tablename)) { 
                            $this->content_db = pc_base::load_model('yp_content_model'); 
                            $this->content_db->set_model($modelid); 
  
                        } 
                        $datas = $this->content_db->select($where, '*'); 
                    } 
                    $data = array_merge($data,$datas); 
                } 
                $pages = $this->db->pages; 
                $totalnums = $this->db->number; 
            
                //如果分词结果为空 
                if(!emptyempty($segment_q)) { 
                    $replace = explode(' ', $segment_q); 
                    foreach($replace as $replace_arr_v) { 
                        $replace_arr[] =  ''.$replace_arr_v.''; 
                    } 
                    foreach($data as $_k=>$_v) { 
                        $data[$_k]['title'] = str_replace($replace, $replace_arr, $_v['title']); 
                        $data[$_k]['description'] = str_replace($replace, $replace_arr, $_v['description']); 
                    } 
                } else { 
                    foreach($data as $_k=>$_v) { 
                        $data[$_k]['title'] = str_replace($q, ''.$q.'', $_v['title']); 
                        $data[$_k]['description'] = str_replace($q, ''.$q.'', $_v['description']); 
                    } 
                } 
            } 
            $execute_time = execute_time(); 
            $pages = isset($pages) ? $pages : ''; 
            $totalnums = isset($totalnums) ? $totalnums : 0; 
            $data = isset($data) ? $data : ''; 
             
            include    template('search','list'); 
        } else { 
            include    template('search','index'); 
        } 
    } 
  
     
    public function public_get_suggest_keyword() { 
        $url = $_GET['url'].'&q='.$_GET['q']; 
        $trust_url = array('c8430fcf851e85818b546addf5bc4dd3'); 
        $urm_md5 = md5($url); 
        if (!in_array($urm_md5, $trust_url)) exit; 
         
        $res = @file_get_contents($url); 
        if(CHARSET != 'gbk') { 
            $res = iconv('gbk', CHARSET, $res); 
        } 
        echo $res; 
    } 
     
    /** 
     * 提示搜索接口 
     * TODO 暂时未启用,用的是google的接口 
     */ 
    public function public_suggest_search() { 
        //关键词转换为拼音 
        pc_base::load_sys_func('iconv'); 
        $pinyin = gbk_to_pinyin($q); 
        if(is_array($pinyin)) { 
            $pinyin = implode('', $pinyin); 
        } 
        $this->keyword_db = pc_base::load_model('search_keyword_model'); 
        $suggest = $this->keyword_db->select("pinyin like '$pinyin%'", '*', 10, 'searchnums DESC'); 
         
        foreach($suggest as $v) { 
            echo $v['keyword']."\n"; 
        } 
  
         
    } 

?> 

然后在header.html模板上面增加一个“不限”的搜索条件,typeid对应的值为0,search中的index.html和lists.html也做相同处理,效果如本站,这样只要不选择模型那么搜索出来的结果就是所有模型中符合条件的数据

注意:因为复制代码可能导致代码错误,请使用下面的压缩包中的文件

index.php下载:http://www.pweb123.com/uploadfile/2015/0709/20150709103942742.rar


网上还有一种就是修改成模糊搜索的方法:(本方法站长未测试,请大家自行测试)

hpcms v9搜索结果不全及搜索不到内容解决方案:
打开phpcms\modules\search\index.php找到
if(!empty($segment_q)) {
$sql = "`siteid`= '$siteid' AND `typeid` = '$typeid' $sql_time AND MATCH (`data`) AGAINST ('$segment_q' IN BOOLEAN MODE)";
} else {
$sql = "`siteid`= '$siteid' AND `typeid` = '$typeid' $sql_time AND `data` like '%$q%'";
}
替换为
$sql = "`siteid`= '$siteid' AND `typeid` = '$typeid' $sql_time AND `data` like '%$q%'";

就是不再使用分词进行关键字搜索,而是直接使用你输入的关键字查找。
您看到此篇文章时的感受是:
Tags:phpcms 模型 全站 搜索 无法 解决方案 责任编辑:pczzw
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇phpcms 完美实现 导航栏当前栏目.. 下一篇PHPCMS二次开发之表单类使用指南

论坛推荐图文

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

相关栏目

最新文章

热门文章

推荐文章

相关文章