易優全站搜索改造,標題和內容雙重搜索完善記錄!
之前發現利用數據庫查詢語句可以從數據庫直接讀取內容,很方便,于是改寫了下sql語句從內容里面搜索關鍵詞,但是覺得標題搜索也應該考慮進來,于是決定利用union聯合查詢,發現可以。sql語句如下:
{eyou:assign name="keywords" value="$eyou.field.keywords"}
{eyou:assign name="n" value="$eyou.field.page|diy_page"}//自定義函數處理頁碼,后面sql語句會用到
{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}條結果
{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]" }//實用limit限制查詢條數,防止數據過多數據庫服務器崩潰
{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}
查詢結果如下圖所示

但是這樣還不完善,還要有分頁顯示才行,于是改進如下:
<!-- 分頁開始-->
<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">首頁</a></li>
{eyou:assign name="end" value="$field5.count|diy_count"}
{eyou:if condition='($eyou.field.page==""&&$end>1)'}
<li><a>上一頁</a></li>
<li> <a href="/search.html?keywords={$eyou.field.keywords}&submit=Search&page={$eyou.field.page+2}">下一頁</a></li>
{eyou:elseif condition='($eyou.field.page=="1"&&$end>1)'}
<li><a>上一頁</a></li>
<li> <a href="/search.html?keywords={$eyou.field.keywords}&submit=Search&page={$eyou.field.page+1}">下一頁</a></li>
{eyou:elseif condition='($eyou.field.page==""&&$end==1)'}
<li><a>上一頁</a></li>
<li><a>下一頁</a></li>
{eyou:elseif condition='($eyou.field.page=="1"&&$end==1)'}
<li><a>上一頁</a></li>
<li><a>下一頁</a></li>
{eyou:else/}
<li><a href="/search.html?keywords={$eyou.field.keywords}&submit=Search&page={$eyou.field.page-1}">上一頁</a></li>
<li> <a href="/search.html?keywords={$eyou.field.keywords}&submit=Search&page={$eyou.field.page+1}">下一頁</a></li>
{/eyou:if}
<li><a href="/search.html?keywords={$eyou.field.keywords}&submit=Search&page={$end}">末頁</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} 頁</li>
<li>跳轉到 <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('請輸入要跳轉的頁碼',{icon:6});}else if(this.value<1||this.value>{$end}){layer.alert('不存在的頁碼',{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'"> 頁</a></li>
</ul>
</div>
<!-- 分頁結束-->
分頁好了,但是分頁太長的話全部展示不好看啊,而且點擊進入某頁后,當前頁碼需要高亮顯示,再改進下!
<script>
var totalpage=document.querySelectorAll(".page a").length;
var currentpage='{$eyou.field.page}';
if (currentpage==''){
var currentpage=1;
}
var listpage=5;//展示頁碼數量,最好為奇數
var center=Math.ceil((listpage-1)/2);//中間頁
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";
//當前頁大于展示頁小于總頁,當前頁之前的頁碼隱藏
if( currentpage>=listpage && currentpage<totalpage){
for(var i=0;i<currentpage;i++){
document.querySelectorAll(".page")[i].style="display:none";
}
//當前頁之前之后的頁碼展示
for(var k=0;k<=center+1;k++){
document.querySelectorAll(".page")[currentpage-k].style="display:inline-block";
}
}
//當前頁為末頁或大于末頁時,展示末頁之前的listpage頁
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>
以上樣式均為官方自帶模板,可以結合自己的樣式改造,復制相應的模板代碼填充到合適位置即可!
最終效果如下圖:
