易優(yōu)全站搜索改造,標(biāo)題和內(nèi)容雙重搜索完善記錄!
之前發(fā)現(xiàn)利用數(shù)據(jù)庫(kù)查詢語(yǔ)句可以從數(shù)據(jù)庫(kù)直接讀取內(nèi)容,很方便,于是改寫(xiě)了下sql語(yǔ)句從內(nèi)容里面搜索關(guān)鍵詞,但是覺(jué)得標(biāo)題搜索也應(yīng)該考慮進(jìn)來(lái),于是決定利用union聯(lián)合查詢,發(fā)現(xiàn)可以。sql語(yǔ)句如下:
{eyou:assign name="keywords" value="$eyou.field.keywords"}
{eyou:assign name="n" value="$eyou.field.page|diy_page"}//自定義函數(shù)處理頁(yè)碼,后面sql語(yǔ)句會(huì)用到
{eyou:sql sql="select count(*) as count from(select aid from ey_article_content where content like '%$keywords%' union select aid from ey_archives where title like '%$keywords%' union select aid from ey_product_content where content like '%$keywords%') as count" id="field5"}共{$field5.count}條結(jié)果
{eyou:sql sql="select aid from ey_article_content where content like '%$keywords%' union select aid from ey_archives where title like '%$keywords%' union select aid from ey_product_content where content like '%$keywords%' limit $n[0],$n[1]" }//實(shí)用limit限制查詢條數(shù),防止數(shù)據(jù)過(guò)多數(shù)據(jù)庫(kù)服務(wù)器崩潰
{eyou:assign name="aid" value="$field.aid"}
{eyou:sql sql="select aid from ey_archives where aid='$aid'" id="field3"}
<li class="dot">
{eyou:arcview aid="$field3.aid" id="field2" addfields="content" }
<a href="{$field2.arcurl}" title="{$field2.title}"><h1>{$keywords,"<font color='red'>$keywords</font>",$field2.title|str_replace}</h1></a>
{eyou:assign name="neirong" value="$field2.content|html_msubstr"}
<div>{$neirong,$keywords|diy_content}</div>
<span>{$field2.add_time|MyDate='Y-m-d',###}</span>
</li>
{/eyou:arcview}
{/eyou:sql}
{/eyou:sql}
{/eyou:sql}
查詢結(jié)果如下圖所示
但是這樣還不完善,還要有分頁(yè)顯示才行,于是改進(jìn)如下:
<!-- 分頁(yè)開(kāi)始-->
<div class="xl12 xs12 xm12 bm12 text-center">
<div class="blank-small"></div>
<ul class="pagination">
<li><a href="/search.html?keywords={$eyou.field.keywords}&submit=Search&page=1">首頁(yè)</a></li>
{eyou:assign name="end" value="$field5.count|diy_count"}
{eyou:if condition='($eyou.field.page==""&&$end>1)'}
<li><a>上一頁(yè)</a></li>
<li> <a href="/search.html?keywords={$eyou.field.keywords}&submit=Search&page={$eyou.field.page+2}">下一頁(yè)</a></li>
{eyou:elseif condition='($eyou.field.page=="1"&&$end>1)'}
<li><a>上一頁(yè)</a></li>
<li> <a href="/search.html?keywords={$eyou.field.keywords}&submit=Search&page={$eyou.field.page+1}">下一頁(yè)</a></li>
{eyou:elseif condition='($eyou.field.page==""&&$end==1)'}
<li><a>上一頁(yè)</a></li>
<li><a>下一頁(yè)</a></li>
{eyou:elseif condition='($eyou.field.page=="1"&&$end==1)'}
<li><a>上一頁(yè)</a></li>
<li><a>下一頁(yè)</a></li>
{eyou:else/}
<li><a href="/search.html?keywords={$eyou.field.keywords}&submit=Search&page={$eyou.field.page-1}">上一頁(yè)</a></li>
<li> <a href="/search.html?keywords={$eyou.field.keywords}&submit=Search&page={$eyou.field.page+1}">下一頁(yè)</a></li>
{/eyou:if}
<li><a href="/search.html?keywords={$eyou.field.keywords}&submit=Search&page={$end}">末頁(yè)</a></li>
{eyou:for start='1' end='$end+1'}
{eyou:if condition='($i==$eyou.field.page)'}
<li class="page"><a style='color: #fff;background:#ff9600' href="/search.html?keywords={$eyou.field.keywords}&submit=Search&page={$i}">{$i}</a></li>
{eyou:elseif condition='($eyou.field.page==""&&$i==$eyou.field.page+1)'}
<li class="page"><a style='color: #fff;background:#ff9600' href="/search.html?keywords={$eyou.field.keywords}&submit=Search&page={$i}">{$i}</a></li>
{eyou:else/}
<li class="page"><a href="/search.html?keywords={$eyou.field.keywords}&submit=Search&page={$i}">{$i}</a></li>
{/eyou:if}
{/eyou:for}
<li id="more" style="display:none"><a id="a" href="javascript:showmore()">···</a></li>
<li>共 {$end} 頁(yè)</li>
<li>跳轉(zhuǎn)到 <input type='number' oninput="value=value.replace(/[^\d]/g,'')" style="border-radius:8px;width:70px;height:30px;border:1px solid grey" onfocus="style='border-radius:8px;width:70px;height:30px;border:2px solid #FF9600;'" onblur="if(this.value==''){layer.alert('請(qǐng)輸入要跳轉(zhuǎn)的頁(yè)碼',{icon:6});}else if(this.value<1||this.value>{$end}){layer.alert('不存在的頁(yè)碼',{icon:5});}else{window.location.href='/search.html?keywords={$eyou.field.keywords}&submit=Search&page='+this.value+''} style='border-radius:8px;width:70px;height:30px;border:1px solid grey'"> 頁(yè)</a></li>
</ul>
</div>
<!-- 分頁(yè)結(jié)束-->
分頁(yè)好了,但是分頁(yè)太長(zhǎng)的話全部展示不好看啊,而且點(diǎn)擊進(jìn)入某頁(yè)后,當(dāng)前頁(yè)碼需要高亮顯示,再改進(jìn)下!
<script>
var totalpage=document.querySelectorAll(".page a").length;
var currentpage='{$eyou.field.page}';
if (currentpage==''){
var currentpage=1;
}
var listpage=5;//展示頁(yè)碼數(shù)量,最好為奇數(shù)
var center=Math.ceil((listpage-1)/2);//中間頁(yè)
function page(){
if (totalpage>listpage){
for(var i=listpage;i<totalpage;i++)
{
document.querySelectorAll(".page")[i].style="display:none";
}
document.getElementById("more").style="display:inline-block";
//當(dāng)前頁(yè)大于展示頁(yè)小于總頁(yè),當(dāng)前頁(yè)之前的頁(yè)碼隱藏
if( currentpage>=listpage && currentpage<totalpage){
for(var i=0;i<currentpage;i++){
document.querySelectorAll(".page")[i].style="display:none";
}
//當(dāng)前頁(yè)之前之后的頁(yè)碼展示
for(var k=0;k<=center+1;k++){
document.querySelectorAll(".page")[currentpage-k].style="display:inline-block";
}
}
//當(dāng)前頁(yè)為末頁(yè)或大于末頁(yè)時(shí),展示末頁(yè)之前的listpage頁(yè)
if(currentpage>=totalpage){
for(var i=0;i<currentpage;i++){
document.querySelectorAll(".page")[i].style="display:none";
}
for(var k=0;k<listpage;k++){
document.querySelectorAll(".page")[currentpage-k-1].style="display:inline-block";
}
document.getElementById("more").style="display:none";
}
}
else{
for(var i=listpage;i<totalpage;i++)
{
document.querySelectorAll(".page")[i].style="display:inline-block";
}
}
}
function showmore(){
for(var i=0;i<totalpage;i++)
{
document.querySelectorAll(".page")[i].style="display:inline-block";
document.getElementById("a").href="javascript:showless()";
}
}
function showless(){
page();
document.getElementById("a").href="javascript:showmore()";
}
</script>
以上樣式均為官方自帶模板,可以結(jié)合自己的樣式改造,復(fù)制相應(yīng)的模板代碼填充到合適位置即可!
最終效果如下圖: