摘要:
在建站过种中经常看到多字段内容筛选的功能,昨天分享了一个极致CMS多字段内容筛选的实现方法,今天...
描述:
在建站过种中经常看到多字段内容筛选的功能,昨天分享了一个极致CMS多字段内容筛选的实现方法,今天黑猫查查网再分享一个YzmCMS内容筛选的实现方法与详细代码。
首先找到文件:/application/index/controller/index.class.php,找到搜索函数,下面增加一个函数,
/** * 精确搜索/多条件 */public function initpo(){ $site = get_config(); $modelid = isset($_GET['modelid']) ? intval($_GET['modelid']) : 1; $modelinfo = get_modelinfo(); $modelarr = array(); foreach($modelinfo as $val){ $modelarr[$val['modelid']] = $val['tablename']; } if(!isset($modelarr[$modelid])) showmsg('模型不存在!', 'stop'); /** *加入搜索条件 */ $where ="`status` = 1"; $hangye_array = array('1'=>'工业','2'=>'互联网','3'=>'农业'); $guoj_array = array('1'=>'中国','2'=>'日本','3'=>'法国'); $hangye = str_replace('%', '', new_html_special_chars(strip_tags(trim($_GET['hangye'])))); $guoj = str_replace('%', '', new_html_special_chars(strip_tags(trim($_GET['guoj'])))); $qq1 = str_replace('%', '', new_html_special_chars(strip_tags(trim($hangye_array[$hangye])))); $qq2 = str_replace('%', '', new_html_special_chars(strip_tags(trim($guoj_array[$guoj])))); if(isset($hangye)&&($hangye!=0)) $where .= " AND hangye= '$qq1' "; if(isset($guoj)&&($guoj!=0)) $where .= " AND guoj= '$qq2' "; $db = D($modelarr[$modelid]); $total = $db->where($where)->total(); $page = new page($total, $this->offset); $search_data = $db->field('id,title,description,inputtime,updatetime,click,thumb,nickname,url,catid,flag,color,hangye,guoj')->where($where)->order('id DESC')->limit($page->limit())->select(); $pages = '<span class="pageinfo">共<strong>'.$page->total().'</strong>页<strong>'.$total.'</strong>条记录</span>'.$page->getfull(); include template($this->module, 'searchb'); }
再新建模板文件为searchb.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>{$seo_title}</title> <link href="{STATIC_URL}css/yzm-common.css" rel="stylesheet" type="text/css" /> <link href="{STATIC_URL}css/yzm-style.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="{STATIC_URL}js/jquery-1.8.2.min.js"></script> <script type="text/javascript" src="{STATIC_URL}js/yzm-front.js"></script> <meta name="keywords" content="{$keywords}" /> <meta name="description" content="{$description}" /> <!-- <script src="https://lib.baomitu.com/jquery/2.2.4/jquery.min.js"></script> --> <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css" /> <script language="javascript"> function getQueryString(){ var result = location.search.match(new RegExp("[\?\&][^\?\&]+=[^\?\&]+","g")); if(result == null){ return ""; } for(var i = 0; i < result.length; i++){ result[i] = result[i].substring(1); } return result; } function goSort(name,value){ var string_array = getQueryString(); var oldUrl = (document.URL.indexOf("search")==-1)?document.URL+"/search/index/initpo.html":document.URL; var newUrl; if(string_array.length>0)//如果已经有筛选条件 { var repeatField = false; for(var i=0;i<string_array.length;i++){ if(!(string_array[i].indexOf(name)==-1)){ repeatField = true;//如果有重复筛选条件,替换条件值 newUrl = oldUrl.replace(string_array[i],name+"="+value); } } //如果没有重复的筛选字段 if(repeatField == false){ newUrl = oldUrl+"&"+name+"="+value; } }else{//如果还没有筛选条件 newUrl = oldUrl+"?"+name+"="+value; } //跳转 window.location = newUrl; } function setSelected(name,value){ var all_li = $("#"+name).find("li"); //清除所有li标签的selected类 all_li.each(function(){ $(this).removeClass("selected"); }); //为选中的li增加selected类 all_li.eq(value).addClass("selected"); } $(document).ready(function(){ var string_array = getQueryString(); for(var i=0;i<string_array.length;i++){ var tempArr = string_array[i].split("="); setSelected(tempArr[0],tempArr[1]);//设置选中的筛选条件 } }); </script> <style type="text/css"> .search_text{ overflow:hidden; height:100%; padding-top:5px; padding-bottom:5px;}.search_text h1{ color:#6a6a6a; font-weight:bold; float:left; font-size:14px; margin:0px; padding:0px;}.search_text ul{ margin:0; padding:0; list-style:none; float:left; overflow:hidden; height:100%;}.search_text li{ list-style:none; color:#6a6a6a; float:left; width:80px; padding-left:8px; padding-right:5px; white-space:nowrap}.search_text li a{ list-style:none; color:#6a6a6a;}.search_text li a:hover{ list-style:none; color:#fe8f01; font-weight:bold; text-decoration:underline;}.search_text li.selected{color:#fe8f01; font-weight:bold;}.search_text li.selected a{color:#fe8f01;}.search_text li.selected a:hover{color:#fe8f01;}</style> </head> <body> <div class="search_text" id="hangye"> <h1>行业:</h1> <ul> <li class="selected"><a href="javascript:goSort('hangye',0)" rel="external nofollow" >全部</a></li> <li><a href="javascript:goSort('hangye',1)" rel="external nofollow" >工业</a></li> <li><a href="javascript:goSort('hangye',2)" rel="external nofollow" >互联网</a></li> <li><a href="javascript:goSort('hangye',3)" rel="external nofollow" >农业</a></li> </ul> </div> <div class="search_text" id="guoj"> <h1>颜色 :</h1> <ul> <li class="selected"><a href="javascript:goSort('guoj',0);" rel="external nofollow" >全部</a> </li> <li><a href="javascript:goSort('guoj',1);" rel="external nofollow" >中国</a></li> <li><a href="javascript:goSort('guoj',2);" rel="external nofollow" >日本</a></li> <li><a href="javascript:goSort('guoj',3);" rel="external nofollow" >法国</a></li> </ul> </div> <div class="yzm-content-box yzm-main-left yzm-news-list"> <div class="yzm-title"> <h2>“{$q}” 的搜索结果,共<span>{$total}</span>条</h2> </div> {loop $search_data $v} <div class="yzm-news"> <a href="{$v[url]}" class="yzm-news-img"> <img src="{get_thumb($v['thumb'])}" alt="{$v[title]}" title="{$v[title]}" /> </a> <div class="yzm-news-right"> {if strstr($v['flag'],'1')}<em>顶</em>{/if} <!-- 内容属性 --> <a href="{$v[url]}">{title_color($v['title'], $v['color'])}</a> <p>{$v[description]}</p> <div class="yzm-news-tags"> {php $aid = $v['id'];} {m:get sql="SELECT id,tag FROM yzm_tag_content AS a LEFT JOIN yzm_tag AS b ON a.tagid=b.id WHERE aid=$aid" limit="5" return="tag_data"} {loop $tag_data $val} <a href="{tag_url($val['id'])}" target="_blank" >{$val[tag]}</a> {/loop} </div> </div> </div> {/loop} <div id="page">{$pages}</div> </div> <div class="yzm-main-right"> </div> {m:include "index","footer"}
然后再需要用到内容筛选的模板页面加如下代码:
<script language="javascript"> function getQueryString(){ var result = location.search.match(new RegExp("[\?\&][^\?\&]+=[^\?\&]+","g")); if(result == null){ return ""; } for(var i = 0; i < result.length; i++){ result[i] = result[i].substring(1); } return result; } function goSort(name,value){ var string_array = getQueryString(); var oldUrl = (document.URL.indexOf("search")==-1)?"search/index/initpo.html":document.URL; var newUrl; if(string_array.length>0)//如果已经有筛选条件 { var repeatField = false; for(var i=0;i<string_array.length;i++){ if(!(string_array[i].indexOf(name)==-1)){ repeatField = true;//如果有重复筛选条件,替换条件值 newUrl = oldUrl.replace(string_array[i],name+"="+value); } } //如果没有重复的筛选字段 if(repeatField == false){ newUrl = oldUrl+"&"+name+"="+value; } }else{//如果还没有筛选条件 newUrl = oldUrl+"?"+name+"="+value; } //跳转 window.location = newUrl; } function setSelected(name,value){ var all_li = $("#"+name).find("li"); //清除所有li标签的selected类 all_li.each(function(){ $(this).removeClass("selected"); }); //为选中的li增加selected类 all_li.eq(value).addClass("selected"); } $(document).ready(function(){ var string_array = getQueryString(); for(var i=0;i<string_array.length;i++){ var tempArr = string_array[i].split("="); setSelected(tempArr[0],tempArr[1]);//设置选中的筛选条件 } }); </script><div class="search_text" id="hangye"> <h1>行业:</h1> <ul> <li class="selected"><a href="javascript:goSort('hangye',0)" rel="external nofollow" >全部</a></li> <li><a href="javascript:goSort('hangye',1)" rel="external nofollow" >工业</a></li> <li><a href="javascript:goSort('hangye',2)" rel="external nofollow" >互联网</a></li> <li><a href="javascript:goSort('hangye',3)" rel="external nofollow" >农业</a></li> </ul> </div> <div class="search_text" id="guoj"> <h1>颜色 :</h1> <ul> <li class="selected"><a href="javascript:goSort('guoj',0);" rel="external nofollow" >全部</a> </li> <li><a href="javascript:goSort('guoj',1);" rel="external nofollow" >中国</a></li> <li><a href="javascript:goSort('guoj',2);" rel="external nofollow" >日本</a></li> <li><a href="javascript:goSort('guoj',3);" rel="external nofollow" >法国</a></li> </ul> </div>
THE END
请登录后发表评论