應(yīng)該來(lái)說(shuō),學(xué)會(huì)了如何插入記錄,如何顯示記錄,那么現(xiàn)在簡(jiǎn)單的完整的文章系統(tǒng)、新聞系統(tǒng)和留言系統(tǒng)不成問(wèn)題。那接著下面的問(wèn)題就是:隨著信息內(nèi)容的不段增加,單獨(dú)通過(guò)一張頁(yè)面顯示所有信息是不行也是很不理性的。所以,解決的辦法就是采用分頁(yè)技術(shù)。
1,rs.RecordCount
很顯然,RecordCount就是用來(lái)顯示數(shù)據(jù)庫(kù)表中一共多少條記錄的,也可以形象地說(shuō)表中一共有多少行。經(jīng)常用在分頁(yè)中就是一共有N篇文章等總計(jì)的信息顯示。
2,rs.PageSize
rs.PageSize也就是一頁(yè)的大小,也就表示一張ASP頁(yè)可以顯示記錄的條數(shù)。值是自己定義的,比如經(jīng)常看到的每頁(yè)顯示N篇文章之類(lèi)的信息。
3,rs.AbsolutePage 和 rs.pagecount
說(shuō)到分頁(yè),一定不能不提到 rs.AbsolutePage 。記錄集的AbsolutePage屬性最主要的作用就是決定著當(dāng)前顯示的是第幾頁(yè)。它的值是有依據(jù)的,指定了rs.PageSize,那么rs.pagecount的信息值就是rs.RecordCount和rs.PageSize整除結(jié)果。比如:總信息記錄rs.RecordCount共20條,每頁(yè)顯示條數(shù)rs.PageSize設(shè)為5條,那么頁(yè)數(shù)rs.pagecount數(shù)就是20/5=4頁(yè)次,而rs.AbsolutePage則就只能是第1頁(yè),第2頁(yè)……第4頁(yè)。
說(shuō)到現(xiàn)在,弄個(gè)具體程序來(lái)調(diào)試一下。繼續(xù)對(duì)showit.asp進(jìn)行修改如下:
<!--#include file="conn.asp" -->
<%
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from cnarticle order by cn_id desc"
rs.Open sql,conn,1,1
%>
<%
page=request.querystring("page") 'page值為接受值
rs.PageSize = 2 '每頁(yè)顯示記錄數(shù)
rs.AbsolutePage = Page '顯示當(dāng)前頁(yè)等于接收的頁(yè)數(shù)
%>
<%
For i = 1 to rs.PageSize '利用for next 循環(huán)依次讀出當(dāng)前頁(yè)的記錄
if rs.EOF then
Exit For
end if
response.write("<br>文章內(nèi)容是:"& rs("cn_content"))
rs.MoveNext
next%>
<%
rs.close
Set rs = Nothing
conn.close
set conn=nothing
%>
|
HERE,你調(diào)試的前提就是數(shù)據(jù)庫(kù)中的記錄相對(duì)要大于4條,這樣測(cè)試效果才明顯;還有測(cè)試的方法就是在showit.asp后面添加?page=1或者?page=2等調(diào)試觀(guān)察網(wǎng)頁(yè)顯示效果。
其實(shí),說(shuō)到底,顯示數(shù)據(jù)庫(kù)內(nèi)容就是
<%
For i = 1 to rs.PageSize
if rs.EOF then
Exit For
end if
response.write("<br>文章內(nèi)容是:"& rs("cn_content"))
rs.MoveNext
next%>
|
起的作用,但想象一下:該程序應(yīng)該都只能顯示出2條信息(一直不變的2條信息)。但為什么加上?page=1和?page=2會(huì)顯示不同的結(jié)果呢?……那絕對(duì)就是rs.AbsolutePage的作用了。這個(gè)搞清楚,相信分頁(yè)的整體架構(gòu)就有點(diǎn)眉目了。
<!--#include file="conn.asp" -->
<%
Set rs = Server.CreateObject ("ADODB.Recordset")
sql = "Select * from cnarticle"
rs.Open sql,conn,1,1
%>
<%filepath=request.servervariables("path_info")%>
<%
page=request.querystring("page") 'page值為接受值
rs.PageSize = 2 '每頁(yè)顯示記錄數(shù)
if Not IsEmpty(page) then '如果page已經(jīng)初始化...
if Not IsNumeric(page) then '判斷page值是否為數(shù)字
page=1
else
Page = cint(page) '接收page并化為數(shù)字型賦給page變量
end if
if Page > rs.PageCount then '如果接收的頁(yè)數(shù)大于總頁(yè)數(shù)
rs.AbsolutePage = rs.PageCount '設(shè)置當(dāng)前顯示頁(yè)等于最后頁(yè)
elseif Page <= 0 then '如果page小于等于0
rs.AbsolutePage = 1 '設(shè)置當(dāng)前顯示頁(yè)等于第一頁(yè)
else
rs.AbsolutePage = Page '如果大于零,顯示當(dāng)前頁(yè)等于接收的頁(yè)數(shù)
end if
else
rs.AbsolutePage=1
end if
Page = rs.AbsolutePage%>
<%
For i = 1 to rs.PageSize '利用for next 循環(huán)依次讀出當(dāng)前頁(yè)的記錄
if rs.EOF then
Exit For
end if
response.write("文章標(biāo)題是:"& rs("cn_title"))
response.write("<br>文章作者是:"& rs("cn_author"))
response.write("<br>文章加入時(shí)間是:"& rs("cn_time"))
response.write("<br>文章內(nèi)容是:"& rs("cn_content"))
response.write("<hr>")
rs.MoveNext
Next
%>
<form action="<%=filepath%>" method="get">
<!--首先保證總頁(yè)數(shù)不為1、不為0-->
<%if rs.pagecount<>1 and rs.pagecount<>0 then%>
<!--如果當(dāng)前頁(yè)數(shù)大于1,無(wú)論何時(shí)都應(yīng)顯示首頁(yè)和上一頁(yè)的連接-->
<%if page>1 then%>
[<a Href="<%=filepath%>?Page=<% = 1%>">首頁(yè)</a>]
[<a Href="<%=filepath%>?Page=<% = page -1 %>">上一頁(yè)</a>]
<!--如果當(dāng)前頁(yè)數(shù)大于1并且小于總頁(yè)面數(shù)時(shí),顯示出尾頁(yè)和下一頁(yè)的連接-->
<%if page<rs.pagecount then %>
[<a Href="<%=filepath%>?Page=<% = page + 1%>">下一頁(yè)</a>]
[<a Href="<%=filepath%>?Page=<% = rs.PageCount%>">尾頁(yè)</a>]
<!--如果當(dāng)前頁(yè)數(shù)大于1并且仍大于或等于總頁(yè)面數(shù)時(shí),不顯示出尾頁(yè)和下一頁(yè)的連接-->
<%else%>
[下一頁(yè)] [尾頁(yè)]
<%end if%>
<!--否則,當(dāng)前頁(yè)數(shù)不大于1,則只顯示尾頁(yè)和下一頁(yè)的連接-->
<%else%>
[首頁(yè)] [上一頁(yè)]
[<a Href="<%=filepath%>?Page=<% = page + 1%>">下一頁(yè)</a>]
[<a Href="<%=filepath%>?Page=<% = rs.PageCount%>">尾頁(yè)</a>]
<%end if %>
<!--最終,總頁(yè)數(shù)若為1、為0則沒(méi)有任何連接-->
<%else%>
[首頁(yè)] [上一頁(yè)] [下一頁(yè)] [尾頁(yè)]
<%end if%>
[頁(yè)次:<font color=red><b><%=page%></b></font>/<%=rs.PageCount%>]
[共<%=rs.RecordCount%>篇 <font color=red><b><%=rs.PageSize%></b></font>篇/頁(yè)]
轉(zhuǎn)到<input name="page" size=5 value="<%=page%>">頁(yè)
<input type="submit" value="Enter">
</form>
<%
rs.close
Set rs = Nothing
conn.close
set conn=Nothing
%>
|
效果頁(yè)面參看:
http://www.cnbruce.com/database/
長(zhǎng)長(zhǎng)的分頁(yè)代碼,搞懂是真不容易,平臺(tái)引用起來(lái)還需要修改也是比較麻煩。最后能做成一個(gè)函數(shù),下次調(diào)用起來(lái)就很方便了。
<%
function pagination(pagecount,pagesize,page,resultcount)
Dim query, a, x, temp
action = "http://" & Request.ServerVariables("HTTP_HOST") & Request.ServerVariables("SCRIPT_NAME")
query = Split(Request.ServerVariables("QUERY_STRING"), "&")
For Each x In query
a = Split(x, "=")
If StrComp(a(0), "page", vbTextCompare) <> 0 Then
temp = temp & a(0) & "=" & a(1) & "&"
End If
Next
Response.Write("<form method=get onsubmit=""document.location = '" & action & "?" & temp & "Page='+this.page.value;return false;"">")
if page<=1 then
Response.Write ("[首頁(yè)] [上一頁(yè)] ")
else
Response.Write("[<a href=" & action & "?" & temp & "Page=1>首頁(yè)</a>] ")
Response.Write("[<a href=" & action & "?" & temp & "Page=" & (Page-1) & ">上一頁(yè)</a>] ")
end if
if page>=pagecount then
Response.Write ("[下一頁(yè)] [尾頁(yè)]")
else
Response.Write("[<a href=" & action & "?" & temp & "Page=" & (Page+1) & ">下一頁(yè)</a>] ")
Response.Write("[<a href=" & action & "?" & temp & "Page=" & pagecount & ">尾頁(yè)</a>]")
end if
Response.Write("[頁(yè)次:<font color=red>" & page & "</font>/" & pageCount)
Response.Write("] [共" & resultcount & "條 <font color=red>"& pagesize & "</font>條/頁(yè)]")
Response.Write(" 轉(zhuǎn)到" & "<input name=page size=4 value=" & page & ">" & "頁(yè)<input type=submit value=go>")
End function
%>
|
如要引用,則可以:
<%call pagination(rs.PageCount,rs.pagesize,page,rs.RecordCount) %>