PHPCMSv9全站搜索,不限模型,今天又涨知识了。大家都知道,PHPCMS搜索时按照模型来搜索的,用了这个方法后,就可以全站搜索
简单修改一下v9默认的搜索功能,可以不按模型搜索全站内容
下面是被修改后的search模块中的index.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); } var_dump($result); //如果结果不为空 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, $_v['description']); } } else { foreach($data as $_k=>$_v) { $data[$_k]['Title'] = str_replace($q, ''.$q.'', $_v['Title']); $data[$_k]['description'] = str_replace($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也做相同处理,效果就出来那,本文由程序员人生网www.wfuyu.com整理发布, 这样只要不选择模型那么搜索出来的结果就是所有模型中符合条件的数据
以上是内存溢出为你收集整理的phpcmsv9全站搜索,不限模型全部内容,希望文章能够帮你解决phpcmsv9全站搜索,不限模型所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)