ASP分頁方法集合

<!--#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("<br>文章作者是:"& rs("cn_author"))
response.write("<br>文章加入時間是:"& rs("cn_time"))
response.write("<br>文章內容是:"& 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 οnsubmit=""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,cint(page),rs.RecordCount) %>

前面的代碼是
dim page
rs.pagesize=10
if request.querystring("page")=0 or request.querystring("page")="" then
page=1
else
page=request.querystring("page")
rs.absolutepage=trim(request.querystring("page"))
end if

怎麼樣把最後一個文本框換成列表表單?

Response.Write("轉到<select name=page>")
for i = 1 to pagecount
Response.Write("<option value="&i&">")
next
Response.Write("</select>")

 

一種理論上最快的Web數據庫分頁方法

  上篇我們談到了關於數據庫傳統的三種分頁方法和他們的利弊,並且提出了一種理論上最佳的分頁方法,本篇我們就來詳細說說這種最佳的分頁方法。

一:構思。
  在設計Web數據庫時,如果我們要編歷每一條紀錄,那麼只有採取分頁模式纔可以使Web數據庫儘快,盡好的呈現給終端用戶,也不會因爲8秒原則而使用戶失去瀏覽該頁的興趣。但是即使採取分頁的辦法,當出現多紀錄的數據庫時,也難免會使我們的用戶感到翻頁時速度太慢。就如同我的上篇文章說的那樣,幾乎上一片文章的三種分頁方法都有一些缺陷。那麼,我們如何做到能讓數據庫每次就取我們需要的紀錄,這個很好實現,有遊標返回多個紀錄集就可以實現,但是如果讓數據庫的一端不會因爲要剛好檢索一頁的紀錄而大耗資源就很難了。最後,經過我的不斷改寫程序與測試,終於編寫出了我認爲理論上最快的Web數據庫分頁方法。

二:具體實現的存儲過程。
  我們結合一個BBS問題來談談這種方法。如何讓一個BBS每次每頁只現實需要的一頁紀錄呢?而我們需要提供給數據庫有那些參數呢?可能會有以下參數。
  第一:就是我們需要的當前的頁數。
  第二:當前定義的每一頁的紀錄集數目。這樣你就可以根據需要在頁面程序中修改每一頁的紀錄數。當然,如果你不考慮程序的可擴展性,你也可以在數據庫裏直接規定每一頁有N條紀錄即可。
  第三:一個輸出參數:就是從數據庫裏得出當前表中總紀錄數目的多少。(注意,他不是一個頁的紀錄數目)他相當於ADO分頁法中的Recordcount。如果你不需要總紀錄數目可以不用返回他。
  我們來看具體存儲過程的代碼。。。

CREATE PROCEDURE dbo.PRO_pageview
(

@tint_tableid tinyint=1,   --這個是BBS的當前版面Id,你可以不用管他。。
@int_pagenow int=0,      
@int_pagesize int=0,
@int_recordcount int=0 output  --就是得出BBS某個版面的總貼數。。

)

AS
set nocount on

declare @int_allid int    
declare @int_beginid int,@int_endid int 
declare @int_pagebegin int, @int_pageend int
  
select @int_allid=count(*) from tab_discuss where tint_level=0 and tint_tableid=@tint_tableid
  select @int_recordcount=@int_allid     --得出該版面的總貼數
      
  declare cro_fastread cursor scroll
  for  select int_id from tab_discuss where tint_level=0 and tint_tableid=@tint_tableid order by int_id desc --這裏定義遊標操作,但是不用臨時紀錄集,而且遊標也不需要全部遍歷所有紀錄集。
  
  open cro_fastread --打開遊標
  select @int_beginid=(@int_pagenow-1)*@int_pagesize+1 得出該頁的第一個紀錄Id
  select @int_endid = @int_beginid+@int_pagesize-1   得出該頁的最後一個紀錄的Id
  
  fetch absolute @int_beginid from cro_fastread into @int_pagebegin 將他的Id傳給一個變量該頁開始的Id
  if @int_endid>@int_allid    --這裏要注意,如果某一頁不足固定頁數的紀錄時。如只有一頁紀錄,而且紀錄少於我們定義的數目。或者是最後一頁時。。。
    fetch last from cro_fastread into @int_pageend  --直接將遊標絕對定位到最後一條紀錄,得出他的id號來。。。
  else
    fetch absolute @int_endid from cro_fastread into @int_pageend
        
  select int_id,tint_level,tint_children,var_face,var_subject,datalength(txt_content) as int_len,sint_hits,var_url,var_image,var_user,dat_time,tint_tableid,bit_kernul from tab_discuss where tint_tableid=@tint_tableid and int_rootid between @int_pageend and @int_pagebegin order by int_rootid desc,num_order desc   --我們就可以利用該頁的第一個id和最後一個id得出中間的id來。。。。(注意。我們這個BBS的數性結構用了一種很巧妙的算法,就是用一個orderNum浮點數即可完成排序。。。)

--開始清場。。。
  close cro_fastread     
  deallocate cro_fastread
  
  return


我們再看看Asp頁面裏的程序操作。。。

pagenow=cint(request("pagenow")) --當前的頁面。

if pagenow<=0 then pagenow=1
pagesize=10

set cmd=server.CreateObject("adodb.command")
cmd.ActiveConnection=strconn
cmd.CommandType=4
cmd.CommandText="pro_pageview"

cmd.Parameters.Append cmd.CreateParameter("tint_tableid",adInteger,adParamInput,,tint_tableid)
cmd.Parameters.Append cmd.CreateParameter("int_pagenow",adInteger,adParamInput,,pagenow)
cmd.Parameters.Append cmd.CreateParameter("int_pagesize",adInteger,adParamInput,,pagesize)
cmd.Parameters.Append cmd.CreateParameter("int_recordcount",adInteger,adParamOutput)

set rs=cmd.Execute
if rs.eof then
  Response.Write "目前已超過了紀錄個數或紀錄集爲空!"
  Response.End
end if

dim arrRs
arrRs=rs.getrows   '可以利用getRows將紀錄集迅速保存到一個二維的數組中來提高速度。

recordcount=cmd.Parameters("int_recordcount")
'注意,當紀錄不足以剛好整除單位頁紀錄時,我們也要將其定義爲一頁,如紀錄數目爲2頁多一個紀錄,此時我們的頁數也要爲3頁紀錄。
if (recordcount mod pagesize)=0 then
  pagecount=recordcount/pagesize
else
  pagecount=recordcount/pagesize+1
end if


<--分頁開始 -->
<!-- #include file="include/tablepage.asp" -->固定的分頁函數,其實無非是pagenow+1或pagenow-1,pagenow,pagecount
<!--分頁結束-->


<div align="left" class="pblank" >
<%
'---------顯示樹性結構!-------------
level=0
Response.Write "<ul>"
for i=0 to ubound(arrRs,2)
  if arrRs(1,i)>level then
    Response.Write "<ul>"
  end if
  if arrRs(1,i)<level then
    for j=arrRs(1,i) to level-1
        Response.Write "</ul>"
    next
  end if
  int_size=arrRs(5,i)
  if int_size=0 then
    str_size=" <無內容>"
  else
    str_size=""
  end if
  Response.Write "<li><img src=face/"&arrRs(3,i)&"><a href=showTitle.asp?int_id="&arrRs(0,i)&"&tint_tableid="&tint_tableid&" class=ptitle target=BoardAnnounce>"&server.HTMLEncode(arrRs(4,i))&"</a> "&str_size
  if arrRs(7,i)<>"" then Response.Write " <連接> "
  if arrRs(8,i)<>"" then Response.Write " <圖像> "
  Response.Write "   -【"&arrRs(9,i)&"】 <font color=444444>"&arrRs(10,i)&"</font> [<font color=920092>ID:"&arrRs(0,i)&" 點擊:"&arrRs(6,i)&"次</font>] <font color=444444>("&int_size&"字節)</font> <font color=ff0000>("&arrRs(2,i)&")</font></li>"
  
  
  level=arrRs(1,i)
  
next

Response.Write "</ul>"
'---------顯示樹性結構完畢!-------------

%>
</div>
<div align="left">
<!--分頁開始 -->
<!-- #include file="include/tablepage.asp" -->
<!--分頁結束-->
</div>
<!-- 清場與除錯 -->
<%
rs.close
set rs=nothing
set cmd=nothing
if err.number<>0 then Response.Redirect "bug.asp"
%>

三:特點
  我們來看看他和傳統的三種方法的區別與特點
  第一:每次只傳回一頁紀錄,而且只形成一個紀錄集,而且客戶端可以採用速度最快的火線遊標來完成頁面輸出。而不必像傳統的遊標法用rs.nextrecordset紀錄來輸出紀錄。
  第二:數據庫沒有用到臨時表,這樣比轉儲紀錄集的速度大大提高。
  第三:採用一個滾動遊標,而且遊標只經過二個操作就完成定位。速度也大大提高。
  
  當我採用了這種分頁方法時,已經可以明顯的感覺出分頁速度的提高了。當然,在處理樹型結構,數據庫運算時,我採用了許多方法儘可能的提高速度,如:採用二分區間法來編歷樹型結構,全部採用存儲過程來實現一切sql操作,採用觸發器和數據庫遊標來完成數據庫算法,這樣就儘量避免過多的網絡傳輸。任何操作只和數據庫進行一次參數傳遞就可以完成。


另一種方法就是
pagesize:每頁條數
第n頁

select top "&pagesize&" * from 表名 where id not in (select top "&pagesize*(n-1)&" id from 表名 order by id)order by id

id 是primary key



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章