ADO组件之分页程序

80酷酷网    80kuku.com

  应该来说,学会了如何插入记录,如何显示记录,那么现在简单的完整的文章系统、新闻系统和留言系统不成问题。那接着下面的问题就是:随着信息内容的不段增加,单独通过一张页面显示所有信息是不行也是很不理性的。所以,解决的办法就是采用分页技术。

  1,rs.RecordCount
  很显然,RecordCount就是用来显示数据库表中一共多少条记录的,也可以形象地说表中一共有多少行。经常用在分页中就是一共有N篇文章等总计的信息显示。

  2,rs.PageSize
  rs.PageSize也就是一页的大小,也就表示一张ASP页可以显示记录的条数。值是自己定义的,比如经常看到的每页显示N篇文章之类的信息。

  3,rs.AbsolutePage 和 rs.pagecount
  说到分页,一定不能不提到 rs.AbsolutePage 。记录集的AbsolutePage属性最主要的作用就是决定着当前显示的是第几页。它的值是有依据的,指定了rs.PageSize,那么rs.pagecount的信息值就是rs.RecordCount和rs.PageSize整除结果。比如:总信息记录rs.RecordCount共20条,每页显示条数rs.PageSize设为5条,那么页数rs.pagecount数就是20/5=4页次,而rs.AbsolutePage则就只能是第1页,第2页……第4页。

  说到现在,弄个具体程序来调试一下。继续对showit.asp进行修改如下:


<!--#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 '每页显示记录数
rs.AbsolutePage = Page '显示当前页等于接收的页数
%>

<%
For i = 1 to rs.PageSize '利用for next 循环依次读出当前页的记录
if rs.EOF then
Exit For
end if
response.write("
文章内容是:"& rs("cn_content"))
rs.MoveNext
next%>

<%
rs.close
Set rs = Nothing
conn.close
set conn=nothing
%>

  HERE,你调试的前提就是数据库中的记录相对要大于4条,这样测试效果才明显;还有测试的方法就是在showit.asp后面添加?page=1或者?page=2等调试观察网页显示效果。

  其实,说到底,显示数据库内容就是


<%
For i = 1 to rs.PageSize
if rs.EOF then
Exit For
end if
response.write("
文章内容是:"& rs("cn_content"))
rs.MoveNext
next%>

  起的作用,但想象一下:该程序应该都只能显示出2条信息(一直不变的2条信息)。但为什么加上?page=1和?page=2会显示不同的结果呢?……那绝对就是rs.AbsolutePage的作用了。这个搞清楚,相信分页的整体架构就有点眉目了。


<!--#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 '每页显示记录数
if Not IsEmpty(page) then '如果page已经初始化...
if Not IsNumeric(page) then '判断page值是否为数字
page=1
else
Page = cint(page) '接收page并化为数字型赋给page变量
end if
if Page > rs.PageCount then '如果接收的页数大于总页数
rs.AbsolutePage = rs.PageCount '设置当前显示页等于最后页
elseif Page <= 0 then '如果page小于等于0
rs.AbsolutePage = 1 '设置当前显示页等于第一页
else
rs.AbsolutePage = Page '如果大于零,显示当前页等于接收的页数
end if
else
rs.AbsolutePage=1
end if
Page = rs.AbsolutePage%>

<%
For i = 1 to rs.PageSize '利用for next 循环依次读出当前页的记录
if rs.EOF then
Exit For
end if
response.write("文章标题是:"& rs("cn_title"))
response.write("
文章作者是:"& rs("cn_author"))
response.write("
文章加入时间是:"& rs("cn_time"))
response.write("
文章内容是:"& rs("cn_content"))
response.write("<hr>")
rs.MoveNext
Next
%>

<form action="<%=filepath%>" method="get">
<!--首先保证总页数不为1、不为0-->
<%if rs.pagecount<>1 and rs.pagecount<>0 then%>
<!--如果当前页数大于1,无论何时都应显示首页和上一页的连接-->
<%if page>1 then%>
[<a Href="<%=filepath%>?Page=<% = 1%>">首页</a>]
[<a Href="<%=filepath%>?Page=<% = page -1 %>">上一页</a>]
<!--如果当前页数大于1并且小于总页面数时,显示出尾页和下一页的连接-->
<%if page<rs.pagecount then %>
[<a Href="<%=filepath%>?Page=<% = page + 1%>">下一页</a>]
[<a Href="<%=filepath%>?Page=<% = rs.PageCount%>">尾页</a>]
<!--如果当前页数大于1并且仍大于或等于总页面数时,不显示出尾页和下一页的连接-->
<%else%>
[下一页] [尾页]
<%end if%>
<!--否则,当前页数不大于1,则只显示尾页和下一页的连接-->
<%else%>
[首页] [上一页]
[<a Href="<%=filepath%>?Page=<% = page + 1%>">下一页</a>]
[<a Href="<%=filepath%>?Page=<% = rs.PageCount%>">尾页</a>]
<%end if %>
<!--最终,总页数若为1、为0则没有任何连接-->
<%else%>
[首页] [上一页] [下一页] [尾页]
<%end if%>

[页次:<font color=red><b><%=page%></b></font>/<%=rs.PageCount%>]
[共<%=rs.RecordCount%>篇 <font color=red><b><%=rs.PageSize%></b></font>篇/页]
转到<input name="page" size=5 value="<%=page%>">页
<input type="submit" value="Enter">
</form>

<%
rs.close
Set rs = Nothing
conn.close
set conn=Nothing
%>

  长长的分页代码,搞懂是真不容易,平台引用起来还需要修改也是比较麻烦。最后能做成一个函数,下次调用起来就很方便了。


<%
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 ("[首页] [上一页] ")
    else        
        Response.Write("[<a href=" & action & "?" & temp & "Page=1>_
首页</a>] ")
        Response.Write("[<a href=" & action & "?" & temp & "Page="_
& (Page-1) & ">上一页</a>] ")
    end if

    if page>=pagecount then
        Response.Write ("[下一页] [尾页]")        
    else
        Response.Write("[<a href=" & action & "?" & temp & "Page="_
& (Page+1) & ">下一页</a>] ")
        Response.Write("[<a href=" & action & "?" & temp & "Page="_
& pagecount & ">尾页</a>]")            
    end if
    Response.Write("[页次:<font color=red>" & page & "</font>/" & pageCount)    
    Response.Write("] [共" & resultcount & "条 <font color=red>"& pagesize_
& "</font>条/页]")
    Response.Write(" 转到" & "<input name=page size=4 value=" & page & ">"_
& "页<input type=submit value=go>")
End function
%>

  如要引用,则可以:


<%call pagination(rs.PageCount,rs.pagesize,page,rs.RecordCount) %>

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: