您好,登錄后才能下訂單哦!
這篇文章主要介紹了設置phpcms不限模型搜索的方法,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
phpcmsv9全站搜索,不限模型!
phpcms不限模型搜索的設置方法
phpcmsv9全站搜索,不限模型,今天又漲知識了。大家都知道,phpcms搜索時按照模型來搜索的,用了這個方法后,就可以全站搜索
簡單修改一下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 = empty($_GET['typeid']) ? 0 : intval($_GET['typeid']); $time = empty($_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(!empty($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); } var_dump($result); //如果結果不為空 if(!empty($result) || !empty($commend['id'])) { foreach($result as $_v) { if($_v['typeid']) $sids[$_v['typeid']][] = $_v['id']; } if(!empty($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(empty($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(!empty($segment_q)) { $replace = explode(' ', $segment_q); foreach($replace as $replace_arr_v) { $replace_arr[] = '<font color=red>'.$replace_arr_v.'</font>'; } 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, '<font color=red>'.$q.'</font>', $_v['title']); $data[$_k]['description'] = str_replace($q, '<font color=red>'.$q.'</font>', $_v['description']); } } } $execute_time = execute_time(); $pages = isset($pages) ? $pages : ''; $totalnums = isset($totalnums) ? $totalnums : 0; $data = isset($data) ? $data : ''; includetemplate('search','list'); } else { includetemplate('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也做相同處理,效果就出來那,這樣只要不選擇模型那么搜索出來的結果就是所有模型中符合條件的數據。
感謝你能夠認真閱讀完這篇文章,希望小編分享設置phpcms不限模型搜索的方法內容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。