PHP全模块搜索功能实现方法

11月 23, 2014 phpcms

最近因为仿一个站 其他效果功能基本都OK了 只有搜索还不够完美 PHPcms原始的搜索必须选择模块才能搜索到数据,有时候用户搜索时候可能错过了自己想要搜索的数据!!而且用户体验不理想。
研究了几个小时 终于解决! 我这个写的比较详细!程序小白基本也能完美理解!有任何疑问 直接回复我会尽快回复大家!

1
<?php
1
defined(
1
'IN_PHPCMS'
1
) or exit(
1
'No permission resources.'
1
);
1
pc_base::load_sys_class(
1
'form'
1
,
1
''
1
,0);
1
pc_base::load_sys_class(
1
'format'
1
,
1
''
1
,0);
1
class index {
1
    
1
function

 

1
__construct() {
1
        
1
$
1
this
1
->db = pc_base::load_model(
1
'search_model'
1
);
1
        
1
$
1
this
1
->content_db = pc_base::load_model(
1
'content_model'
1
);
1
    
1
}
1
    

 

1
    
1
/**
1
     
1
* 关键词搜索
1
     
1
*/
1
    
1
public 
1
function

 

1
init() {
1
        
1
//获取siteid
1
        
1
$siteid = isset($_REQUEST[
1
'siteid'
1
]) && trim($_REQUEST[
1
'siteid'
1
]) ? intval($_REQUEST[
1
'siteid'
1
]) : 1;
1
        
1
$SEO = seo($siteid);
 
1
        
1
//搜索配置
1
        
1
$search_setting = getcache(
1
'search'
1
);
1
        
1
$setting = $search_setting[$siteid];
 
1
        
1
$search_model = getcache(
1
'search_model_'
1
.$siteid);
1
        
1
$type_module = getcache(
1
'type_module_'
1
.$siteid);
 
1
        
1
if
1
(isset($_GET[
1
'q'
1
])) {
1
            
1
if
1
(trim($_GET[
1
'q'
1
])==
1
''
1
) {
1
                
1
header(
1
'Location: '
1
.APP_PATH.
1
'index.php?m=search'
1
);exit;
1
            
1
}
1
            
1
$typeid = empty($_GET[
1
'typeid'
1
]) ? 0 : intval($_GET[
1
'typeid'
1
]);
1
            
1
$time = empty($_GET[
1
'time'
1
]) || !in_array($_GET[
1
'time'
1
],array(
1
'all'
1
,
1
'day'
1
,
1
'month'
1
,
1
'year'
1
,
1
'week'
1
)) ? 
1
'all'

 

1
: trim($_GET[
1
'time'
1
]);
1
            
1
$page = isset($_GET[
1
'page'
1
]) ? intval($_GET[
1
'page'
1
]) : 1;
1
            
1
$pagesize = 10;
1
            
1
$q = safe_replace(trim($_GET[
1
'q'
1
]));
1
            
1
$q = new_html_special_chars(strip_tags($q));
1
            
1
$q = str_replace(
1
'%'
1
, 
1
''
1
, $q); 
1
//过滤'%',用户全文搜索
1
            
1
$search_q = $q;  
1
//搜索原内容
 
1
            
1
$sql_time = $sql_tid = 
1
''
1
;
1
            
1
if
1
($typeid) $sql_tid = 
1
' AND typeid = '
1
.$typeid;
1
            
1
//按时间搜索
1
            
1
if
1
($time == 
1
'day'
1
) {
1
                
1
$search_time = SYS_TIME - 86400;
1
                
1
$sql_time = 
1
' AND adddate > '
1
.$search_time;
1
            
1
} elseif($time == 
1
'week'
1
) {
1
                
1
$search_time = SYS_TIME - 604800;
1
                
1
$sql_time = 
1
' AND adddate > '
1
.$search_time;
1
            
1
} elseif($time == 
1
'month'
1
) {
1
                
1
$search_time = SYS_TIME - 2592000;
1
                
1
$sql_time = 
1
' AND adddate > '
1
.$search_time;
1
            
1
} elseif($time == 
1
'year'
1
) {
1
                
1
$search_time = SYS_TIME - 31536000;
1
                
1
$sql_time = 
1
' AND adddate > '
1
.$search_time;
1
            
1
} 
1
else

 

1
{
1
                
1
$search_time = 0;
1
                
1
$sql_time = 
1
''
1
;
1
            
1
}
1
            
1
if
1
($page==1 && !$setting[
1
'sphinxenable'
1
]) {
1
                
1
//精确搜索
1
                
1
$commend = $
1
this
1
->db->get_one(
1
"`siteid`= '$siteid' $sql_tid $sql_time AND `data` like '%$q%'"
1
);
1
            
1
} 
1
else

 

1
{
1
                
1
$commend = 
1
''
1
;
1
            
1
}
1
            
1
//如果开启sphinx
1
            
1
if
1
($setting[
1
'sphinxenable'
1
]) {
1
                
1
$sphinx = pc_base::load_app_class(
1
'search_interface'
1
, 
1
''
1
, 0);
1
                
1
$sphinx = 
1
new

 

1
search_interface();
1
                

 

1
                
1
$offset = $pagesize*($page-1);
1
                
1
$res = $sphinx->search($q, array($siteid), array($typeid), array($search_time, SYS_TIME), $offset, $pagesize, 
1
'@weight desc'
1
);
1
                
1
$totalnums = $res[
1
'total'
1
];
1
                
1
//如果结果不为空
1
                
1
if
1
(!empty($res[
1
'matches'
1
])) {
1
                    
1
$result = $res[
1
'matches'
1
];
1
                
1
}
1
            
1
} 
1
else

 

1
{
1
                

 

1
                
1
$sql = 
1
"`siteid`= '$siteid' $sql_tid $sql_time AND `data` like '%$q%'"
1
;
1
                

 

 
1
                
1
$result = $
1
this
1
->db->listinfo($sql, 
1
'searchid DESC'
1
, $page, 10);
1
            
1
}
1
           

 

1
            
1
//如果结果不为空
1
            
1
if
1
(!empty($result) || !empty($commend[
1
'id'
1
])) {
1
                
1
foreach($result as $_v) {
1
                    
1
if
1
($_v[
1
'typeid'
1
]) $sids[$_v[
1
'typeid'
1
]][] = $_v[
1
'id'
1
];
1
                
1
}
 
1
                
1
if
1
(!empty($commend[
1
'id'
1
])) {
1
                    
1
if
1
($commend[
1
'typeid'
1
]) $sids[$commend[
1
'typeid'
1
]][] = $commend[
1
'id'
1
];
1
                
1
}
1
                
1
$model_type_cache = getcache(
1
'type_model_'
1
.$siteid,
1
'search'
1
);
1
                
1
$model_type_cache = array_flip($model_type_cache);
1
                
1
$data = array();
1
                
1
foreach($sids as $_k=>$_val) {
1
                    
1
$tid = $_k;
1
                    
1
$ids = array_unique($_val);
 
1
                    
1
$where = to_sqls($ids, 
1
''
1
, 
1
'id'
1
);
1
                    
1
//获取模型id
1
                    
1
$modelid = $model_type_cache[$tid];
 
1
                    
1
//是否读取其他模块接口
1
                    
1
if
1
($modelid) {
1
                        
1
$
1
this
1
->content_db->set_model($modelid);
1
                    

 

1
                        
1
/**
1
                        
1
* 如果表名为空,则为黄页模型
1
                        
1
*/
1
                        
1
if
1
(empty($
1
this
1
->content_db->model_tablename)) {
1
                            
1
$
1
this
1
->content_db = pc_base::load_model(
1
'yp_content_model'
1
);
1
                            
1
$
1
this
1
->content_db->set_model($modelid);
 
1
                        
1
}
1
                        
1
$datas = $
1
this
1
->content_db->select($where, 
1
'*'
1
);
1
                    
1
}
1
                    
1
$data = array_merge($data,$datas);
1
                
1
}
1
                
1
$pages = $
1
this
1
->db->pages;
1
                
1
$totalnums = $
1
this
1
->db->number;
1
            

 

1
                
1
//如果分词结果为空
1
                
1
if
1
(!empty($segment_q)) {
1
                    
1
$replace = explode(
1
' '
1
, $segment_q);
1
                    
1
foreach($replace as $replace_arr_v) {
1
                        
1
$replace_arr[] =  
1
'<font color=red>'
1
.$replace_arr_v.
1
'</font>'
1
;
1
                    
1
}
1
                    
1
foreach($data as $_k=>$_v) {
1
                        
1
$data[$_k][
1
'title'
1
] = str_replace($replace, $replace_arr, $_v[
1
'title'
1
]);
1
                        
1
$data[$_k][
1
'description'
1
] = str_replace($replace, $replace_arr, $_v[
1
'description'
1
]);
1
                    
1
}
1
                
1
} 
1
else

 

1
{
1
                    
1
foreach($data as $_k=>$_v) {
1
                        
1
$data[$_k][
1
'title'
1
] = str_replace($q, 
1
'<font color=red>'
1
.$q.
1
'</font>'
1
, $_v[
1
'title'
1
]);
1
                        
1
$data[$_k][
1
'description'
1
] = str_replace($q, 
1
'<font color=red>'
1
.$q.
1
'</font>'
1
, $_v[
1
'description'
1
]);
1
                    
1
}
1
                
1
}
1
            
1
}
1
            
1
$execute_time = execute_time();
1
            
1
$pages = isset($pages) ? $pages : 
1
''
1
;
1
            
1
$totalnums = isset($totalnums) ? $totalnums : 0;
1
            
1
$data = isset($data) ? $data : 
1
''
1
;
1
            

 

1
            
1
include template(
1
'search'
1
,
1
'list'
1
);
1
        
1
} 
1
else

 

1
{
1
            
1
include template(
1
'search'
1
,
1
'index'
1
);
1
        
1
}
1
    
1
}
 
1
    

 

1
    
1
public 
1
function

 

1
public_get_suggest_keyword() {
1
        
1
$url = $_GET[
1
'url'
1
].
1
'&q='
1
.$_GET[
1
'q'
1
];
1
        
1
$trust_url = array(
1
'c8430fcf851e85818b546addf5bc4dd3'
1
);
1
        
1
$urm_md5 = md5($url);
1
        
1
if

 

1
(!in_array($urm_md5, $trust_url)) exit;
1
        

 

1
        
1
$res = @file_get_contents($url);
1
        
1
if
1
(CHARSET != 
1
'gbk'
1
) {
1
            
1
$res = iconv(
1
'gbk'
1
, CHARSET, $res);
1
        
1
}
1
        
1
echo $res;
1
    
1
}
1
    

 

1
    
1
/**
1
     
1
* 提示搜索接口
1
     
1
* TODO 暂时未启用,用的是google的接口
1
     
1
*/
1
    
1
public 
1
function

 

1
public_suggest_search() {
1
        
1
//关键词转换为拼音
1
        
1
pc_base::load_sys_func(
1
'iconv'
1
);
1
        
1
$pinyin = gbk_to_pinyin($q);
1
        
1
if
1
(is_array($pinyin)) {
1
            
1
$pinyin = implode(
1
''
1
, $pinyin);
1
        
1
}
1
        
1
$
1
this
1
->keyword_db = pc_base::load_model(
1
'search_keyword_model'
1
);
1
        
1
$suggest = $
1
this
1
->keyword_db->select(
1
"pinyin like '$pinyin%'"
1
, 
1
'*'
1
, 10, 
1
'searchnums DESC'
1
);
1
        

 

1
        
1
foreach($suggest as $v) {
1
            
1
echo $v[
1
'keyword'
1
].
1
"\n"
1
;
1
        
1
}
 
1
        

 

1
    
1
}
1
}
1
?>

复制上面文件另存为 index.php
覆盖到/phpcms/modules/search/ 下面

然后修改整站 header.html模板
在搜索位置

1
<form action=
1
"{APP_PATH}index.php"

 

1
method=
1
"get"

 

1
target=
1
"_blank"

 

1
id=
1
"search"

 

1
typeid=
1
"0"
1
>

加上typeid=”0″

然后修改/phpcms/templates/default/search/ index.html

1
<script type=
1
"text/javascript"
1
>document.write(
1
'<iframe src="{APP_PATH}index.php?m=member&c=index&a=mini&forward='
1
+encodeURIComponent(location.href)+
1
'&siteid={get_siteid()}+ typeid="0"" allowTransparency="true"  width="300" height="24" frameborder="0" scrolling="no"></iframe>'
1
)</script>

注意 typeid=”0″ 我传递了0参数
至此完美了!

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注