2022-11-10  路径:phpcms/modules/search/index.php(具体内容根据自己详细代码进行针对修改)

1
<?php 2 defined('IN_PHPCMS') or exit('No permission resources.'); 3 pc_base::load_sys_class('form','',0); 4 pc_base::load_sys_class('format','',0); 5 class index { 6 function __construct() { 7 $this->db = pc_base::load_model('search_model'); 8 $this->content_db = pc_base::load_model('content_model'); 9 } 10 11 /** 12 * 关键词搜索 13 */ 14 public function init() { 15 //获取siteid 16 $siteid = isset($_REQUEST['siteid']) && trim($_REQUEST['siteid']) ? intval($_REQUEST['siteid']) : 1; 17 $SEO = seo($siteid); 18 19 //搜索配置 20 $search_setting = getcache('search'); 21 $setting = $search_setting[$siteid]; 22 23 $search_model = getcache('search_model_'.$siteid); 24 $type_module = getcache('type_module_'.$siteid); 25 26 if(isset($_GET['q'])) { 27 if(trim($_GET['q'])=='') { 28 header('Location: '.APP_PATH.'index.php?m=search');exit; 29 } 30 $typeid = empty($_GET['typeid']) ? 48 : intval($_GET['typeid']); 31 $time = empty($_GET['time']) || !in_array($_GET['time'],array('all','day','month','year','week')) ? 'all' : trim($_GET['time']); 32 $page = isset($_GET['page']) ? intval($_GET['page']) : 1; 33 $pagesize = 10; 34 $q = safe_replace(trim($_GET['q'])); 35 $q = new_html_special_chars(strip_tags($q)); 36 $q = str_replace('%', '', $q); //过滤'%',用户全文搜索 37 $search_q = $q; //搜索原内容 38 39 //按时间搜索 40 if($time == 'day') { 41 $search_time = SYS_TIME - 86400; 42 $sql_time = ' AND adddate > '.$search_time; 43 } elseif($time == 'week') { 44 $search_time = SYS_TIME - 604800; 45 $sql_time = ' AND adddate > '.$search_time; 46 } elseif($time == 'month') { 47 $search_time = SYS_TIME - 2592000; 48 $sql_time = ' AND adddate > '.$search_time; 49 } elseif($time == 'year') { 50 $search_time = SYS_TIME - 31536000; 51 $sql_time = ' AND adddate > '.$search_time; 52 } else { 53 $search_time = 0; 54 $sql_time = ''; 55 } 56 if($page==1 && !$setting['sphinxenable']) { 57 //精确搜索 58 $commend = $this->db->get_one("`typeid` = '$typeid' $sql_time AND `data` like '%$q%'"); 59 } else { 60 $commend = ''; 61 } 62 //如果开启sphinx 63 if($setting['sphinxenable']) { 64 $sphinx = pc_base::load_app_class('search_interface', '', 0); 65 $sphinx = new search_interface(); 66 67 $offset = $pagesize*($page-1); 68 $res = $sphinx->search($q, array($siteid), array($typeid), array($search_time, SYS_TIME), $offset, $pagesize, '@weight desc'); 69 $totalnums = $res['total']; 70 //如果结果不为空 71 if(!empty($res['matches'])) { 72 $result = $res['matches']; 73 } 74 } else { 75 pc_base::load_sys_class('segment', '', 0); 76 $segment = new segment(); 77 //2022-11-08 78 //以下代码实现根据文章内容关键字模糊搜索功能202011104 79 $dbhost = '***'; // mysql服务器主机地址 80 $dbuser = '***'; // mysql用户名 81 $dbpass = '***'; // mysql用户名密码 82 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); 83 //print_r($_GET['q']); 84 $qqq = ($_GET['q']); 85 if(! $conn ) 86 { 87 die('连接失败: ' . mysqli_error($conn)); 88 } 89 // 设置编码,防止中文乱码 90 mysqli_query($conn , "set names utf8"); 91 92 $sqlss = "SELECT b.id,title,url,content FROM v9_jjw_article_data a inner join v9_jjw_article b on a.id=b.id where content like '%$qqq%' "; 93 //var_dump($sqlss);die; 94 //print_r($sqlss) 95 //根据内容关键字模糊搜索,得到文章id 96 97 mysqli_select_db( $conn, 'sql_hqjjw2022' ); 98 $retval = mysqli_query( $conn, $sqlss ); 99 //var_dump($retval);die; 100 if(! $retval ) 101 { 102 die('无法读取数据: ' . mysqli_error($conn)); 103 } 104 105 $ids = array(); 106 while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC)){ 107 $ids[] = $row['id']; 108 } 109 $ids_im = implode("," , $ids); 110 //$ids_im =''; 111 //echo $ids_im;die; 112 mysqli_close($conn); 113 //print_r($_GET['q']); 114 //以上代码实现根据文章内容关键字模糊搜索功能202011104 115 //分词结果 116 $segment_q = $segment->get_keyword($segment->split_result($q)); 117 //如果分词结果为空 118 /*if(!empty($segment_q)) { 119 $sql = "`siteid`= '$siteid' AND `typeid` = '$typeid' $sql_time AND MATCH (`data`) AGAINST ('$segment_q' IN BOOLEAN MODE)"; 120 } else { 121 $sql = "`siteid`= '$siteid' AND `typeid` = '$typeid' $sql_time AND `data` like '%$q%'"; 122 }*/ 123 if(empty($ids_im)){ 124 $sql = "`siteid`= '$siteid' AND `typeid` = '$typeid' $sql_time AND (`data` like '%$q%')"; 125 }else{ 126 $sql = "`siteid`= '$siteid' AND `typeid` = '$typeid' $sql_time AND (`data` like '%$q%' or id in ($ids_im))"; 127 } 128 //modify by DongGe更改显示结果多条只显示一条bug 129 //print_r($sql); 130 $result = $this->db->listinfo($sql, 'searchid DESC', $page,15); 131 } 132 133 //如果开启相关搜索功能 134 if($setting['relationenble']) { 135 //如果关键词长度在8-16之间,保存关键词作为relation search 136 $this->keyword_db = pc_base::load_model('search_keyword_model'); 137 138 if(strlen($q) < 17 && strlen($q) > 5 && !empty($segment_q)) { 139 $res = $this->keyword_db->get_one(array('keyword'=>$q)); 140 if($res) { 141 //关键词搜索数+1 142 //$this->keyword_db->update(array('searchnums'=>'+=1'), array('keyword'=>$q)); 143 } else { 144 //关键词转换为拼音 145 pc_base::load_sys_func('iconv'); 146 $pinyin = gbk_to_pinyin($q); 147 if(is_array($pinyin)) { 148 $pinyin = implode('', $pinyin); 149 } 150 $this->keyword_db->insert(array('keyword'=>$q, 'searchnums'=>1, 'data'=>$segment_q, 'pinyin'=>$pinyin)); 151 } 152 } 153 //相关搜索 154 if(!empty($segment_q)) { 155 $relation_q = str_replace(' ', '%', $segment_q); 156 } else { 157 $relation_q = $q; 158 } 159 $relation = $this->keyword_db->select("MATCH (`data`) AGAINST ('%$relation_q%' IN BOOLEAN MODE)", '*', 10, 'searchnums DESC'); 160 } 161 162 //如果结果不为空 163 if(!empty($result) || !empty($commend['id'])) { 164 //开启sphinx后文章id取法不同 165 if($setting['sphinxenable']) { 166 foreach($result as $_v) { 167 $sids[] = $_v['attrs']['id']; 168 } 169 } else { 170 foreach($result as $_v) { 171 $sids[] = $_v['id']; 172 } 173 } 174 175 if(!empty($commend['id'])) { 176 $sids[] = $commend['id']; 177 } 178 $sids = array_unique($sids); 179 180 $where = to_sqls($sids, '', 'id'); 181 //获取模型id 182 $model_type_cache = getcache('type_model_'.$siteid,'search'); 183 $model_type_cache = array_flip($model_type_cache); 184 $modelid = $model_type_cache[$typeid]; 185 186 //是否读取其他模块接口 187 if($modelid) { 188 $this->content_db->set_model($modelid); 189 190 /** 191 * 如果表名为空,则为黄页模型 192 */ 193 if(empty($this->content_db->model_tablename)) { 194 $this->content_db = pc_base::load_model('yp_content_model'); 195 $this->content_db->set_model($modelid); 196 197 } 198 199 //搜索 200 $datas = $this->content_db->select($where, '*'); 201 202 if($setting['sphinxenable']) { 203 $data = $this->content_db->listinfo($where, 'id DESC', 1, $pagesize); 204 $pages = pages($totalnums, $page, $pagesize); 205 } else { 206 $data = $this->content_db->select($where, '*'); 207 $pages = $this->db->pages; 208 $totalnums = $this->db->number; 209 } 210 211 //如果分词结果为空 212 if(!empty($segment_q)) { 213 $replace = explode(' ', $segment_q); 214 foreach($replace as $replace_arr_v) { 215 $replace_arr[] = '<font color=red>'.$replace_arr_v.'</font>'; 216 } 217 foreach($data as $_k=>$_v) { 218 $data[$_k]['title'] = str_replace($replace, $replace_arr, $_v['title']); 219 $data[$_k]['description'] = str_replace($replace, $replace_arr, $_v['description']); 220 } 221 } else { 222 foreach($data as $_k=>$_v) { 223 $data[$_k]['title'] = str_replace($q, '<font color=red>'.$q.'</font>', $_v['title']); 224 $data[$_k]['description'] = str_replace($q, '<font color=red>'.$q.'</font>', $_v['description']); 225 } 226 } 227 } else { 228 //读取专辑搜索接口 229 $special_api = pc_base::load_app_class('search_api', 'special'); 230 $data = $special_api->get_search_data($sids); 231 $totalnums = count($data); 232 } 233 } 234 $execute_time = execute_time(); 235 $pages = isset($pages) ? $pages : ''; 236 $totalnums = isset($totalnums) ? $totalnums : 0; 237 $data = isset($data) ? $data : ''; 238 239 include template('search','list'); 240 } else { 241 include template('search','index'); 242 } 243 } 244 245 246 public function public_get_suggest_keyword() { 247 $url = $_GET['url'].'&q='.$_GET['q']; 248 $trust_url = array('c8430fcf851e85818b546addf5bc4dd3'); 249 $urm_md5 = md5($url); 250 if (!in_array($urm_md5, $trust_url)) exit; 251 252 $res = @file_get_contents($url); 253 if(CHARSET != 'gbk') { 254 $res = iconv('gbk', CHARSET, $res); 255 } 256 echo $res; 257 } 258 259 /** 260 * 提示搜索接口 261 * TODO 暂时未启用,用的是google的接口 262 */ 263 public function public_suggest_search() { 264 //关键词转换为拼音 265 pc_base::load_sys_func('iconv'); 266 $pinyin = gbk_to_pinyin($q); 267 if(is_array($pinyin)) { 268 $pinyin = implode('', $pinyin); 269 } 270 $this->keyword_db = pc_base::load_model('search_keyword_model'); 271 $suggest = $this->keyword_db->select("pinyin like '$pinyin%'", '*', 10, 'searchnums DESC'); 272 273 foreach($suggest as $v) { 274 echo $v['keyword']."\n"; 275 } 276 277 278 } 279 } 280 ?>

 

原文地址:http://www.cnblogs.com/zsyyyds/p/16876190.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性