【ASP】對資料庫查詢的內容分頁顯示演算法的改進

來源:互聯網
上載者:User

標籤:asp   vbscript   資料庫   分頁   改進   

上次寫的《【ASP】資料庫查詢的內容分頁顯示》(點擊開啟連結)中的實現演算法雖然能夠在一個特定的表中實現了預想的效果,

但是這一演算法移植到其他系統之後,發現通用性不強

首先此分頁演算法的具體實施是通過資料庫背景ID號的區分每一頁的,

在一些表中,如果查詢出來的ID結果是亂序的,此演算法則會失效。

同時,翻頁的連結是寫死的,沒有實現宏,來確定此頁的名稱。

所以,必須通過大量的修改,實現此演算法,但是根本的思想還是沒變的,也算是【ASP】資料庫查詢的內容分頁顯示v2吧!

一、基本思想

1.頁面的名稱可以通過程式取到,那麼此頁面放到任意一個位置,被修改成任意的檔案名稱,演算法也不會失效。

2.還是設定好的upper與lower,total,pages,但計算方式有所改變。

3.具體是每一頁都查詢全表記錄,然後,在前台判定那個範圍是應該算出的。

二、製作過程

所實現的功能與《【ASP】資料庫查詢的內容分頁顯示》(點擊開啟連結)是沒有區別的

但是,整體代碼變化更變了不少。具體請看每一行的注釋。

<!--asp頁面必須要有此句,否則頁面亂碼--><%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><!--頁面使用utf-8否則容易亂碼--><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>翻頁</title></head><!--取到本頁的檔案名稱,為後面翻頁的超級連結所使用--><%pagename=Mid(Request.ServerVariables("SCRIPT_NAME"),InStrRev(Request.ServerVariables("SCRIPT_NAME"),"/")+1)%><body><h1>名單:</h1><!--如果首次進入此頁面,則預設id與per為0與1--><%id=Request.QueryString("id")if id="" thenid=0end if%><%per=Request.QueryString("per")if per="" thenper=1end if%><!--lower與upper的計算方式改變了。主要是輸出判斷從後台資料庫改變為前台。--><!--每一頁,後台資料庫都把整張表送過來,但只輸出一部分--><%lower=0upper=0lower=cint(id)*cint(per)upper=cint(id)*cint(per)+cint(per)-1%><%db="../Database.mdb"Set conn = Server.CreateObject("ADODB.Connection")conn.Open "driver={Microsoft Access Driver (*.mdb)};pwd=admin;dbq=" & Server.MapPath(db) %><!--這裡的pages計算方式,根據總記錄數total除以每頁顯示條數per,並取上限得出,--><!--由於asp沒有取上限函數,所以只能加0.999999……這一無限不迴圈小數取整數部分得出--><!--查詢每頁的數目應該用另一個查詢變數rs2,區分查全表變數rs--><%Set rs2 = Server.CreateObject( "ADODB.Recordset" )sql = "select count(*) as total from test;"rs2.open sql,conn,1,3total=rs2("total")pages=int(cint(total)/cint(per)+0.9)%><!--基於上限的計算方式,如果最後一條記錄應該是少於總數加上每頁顯示數目。--><%if cint(upper)<cint(total)+cint(per) and cint(id)>-1 then%><%Set rs = Server.CreateObject( "ADODB.Recordset" )sql = "select * from test;"rs.open sql,conn,1,3%><%if (rs.bof and rs.eof) then%>暫無<!--asp中初始設定變數i的預設值為0--><!--如果i在上限與下限之外就不做任何事,否則輸出--><%elsedo while not rs.eofif cint(i)<cint(lower) or cint(i)>cint(upper) thenelse%><table>  <tr>    <td><%=rs("id")%></a></td>    <td><%=rs("username")%></td>    <td><%=rs("password")%></td>  </tr></table><%end ifi=i+1rs.movenextloop%><!--計算好upper,lower,page,之後就沒有太多的改動了。-->第<select id="page"><%t=0do while cint(t)<cint(pages)%><%if (cint(t)=cint(id)) then%><option value="<%=t+1%>" selected="selected"><%=t+1%></option><%else%><option value="<%=t+1%>"><%=t+1%></option><%end if%><%t=t+1loop%></select>/<%=cint(pages)%>頁<input type="button" onclick="gopage()" value="轉到該頁"/>每頁顯示<a href="<%=pagename%>?id=<%=(id)%>&per=1">1</a>|<a href="<%=pagename%>?id=<%=(id)%>&per=2">2</a>|<a href="<%=pagename%>?id=<%=(id)%>&per=3">3</a>條記錄<a href="<%=pagename%>?id=<%=(id-1)%>&per=<%=(per)%>">上一頁</a><a href="<%=pagename%>?id=<%=(id+1)%>&per=<%=(per)%>">下一頁</a><%end if%><%rs.closeset rs=nothing%><%else%>暫無 <a href="javascript:history.go(-1)">返回</a><%end if%><%rs2.closeset rs2=nothingconn.closeset conn=nothing%></body></html><script>function gopage() {var id=document.getElementById("page").value-1;window.location.href = "<%=pagename%>?id="+id+"&per=<%=(per)%>";} </script>


 

 

【ASP】對資料庫查詢的內容分頁顯示演算法的改進

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.