怎麼實現數據記錄的分頁顯示[轉]

怎麼實現數據記錄的分頁顯示(作者:DarkMan)

怎麼實現數據記錄的分頁顯示 (1)

通過Recordset的GetRows方法,可以實現數據記錄的分頁顯示。下面是一個完整的例子:
<%@ Language = VBSCRIPT %>
<% Option Explicit %>
<%
Dim iStart, iOffset
iStart = Request("Start")
iOffset = Request("Offset")

if Not IsNumeric(iStart) or Len(iStart) = 0 then
iStart = 0
else
iStart = CInt(iStart)
end if

if Not IsNumeric(iOffset) or Len(iOffset) = 0 then
iOffset = 10
else
iOffset = Cint(iOffset)
end if

Response.Write "察看 " & iOffset & " 個記錄從 " & iStart & "開始 <BR>"

Dim objConn, objRS
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "Provider=SQLOLEDB.1;Data Source=(local);uid=sa;pwd=;Initial  Catalog=pubs"

Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open "SELECT * FROM Authors", objConn

Dim aResults
aResults = objRS.GetRows

objRS.Close
Set objRS = Nothing

objConn.Close
Set objConn = Nothing

Dim iRows, iCols, iRowLoop, iColLoop, iStop
iRows = UBound(aResults, 2)
iCols = UBound(aResults, 1)

If iRows > (iOffset + iStart) Then
iStop = iOffset + iStart - 1
Else
iStop = iRows
End If

For iRowLoop = iStart to iStop
For iColLoop = 0 to iCols
 Response.Write aResults(iColLoop, iRowLoop) & " "
 Next
Response.Write "<BR>"
 Next

Response.Write "<P>"
if iStart > 0 then
'顯示“前 10個”連接
Response.Write "<A HREF=""paging.asp?Start=" & iStart-iOffset & _
 "&Offset=" & iOffset & """>前 " & iOffset & "</A>"
 end if

if iStop < iRows then
'顯示“後 10個”連接
 Response.Write " <A HREF=""paging.asp?Start=" & iStart+iOffset & _
"&Offset=" & iOffset & """>後 " & iOffset & "</A>"
end if
%>


怎麼實現數據的分頁顯示(2)
這裏介紹另外一種分頁顯示的方法,是通過 MS SQL的存儲過程。本方法不適用於Access數據庫。
假設我們要對數據表MyTable的數據實現分頁顯示,首先寫一個存儲過程 如下:
CREATE PROCEDURE sp_PagedItems
 (
@Page int,
@RecsPerPage int
)
AS

-- 加快表的 插入速度
SET NOCOUNT ON

-- 開始記錄 號
DECLARE @RecCount int
SELECT @RecCount = @RecsPerPage * @Page + 1

--創建臨時 表
CREATE TABLE #TempItems
(
ID int IDENTITY,
Name varchar(50),
Price currency
 )

-- 準備臨時 表
INSERT INTO #TempItems (Name, Price)
SELECT Name,Price FROM MyTable ORDER BY Price

-- 求出要查 詢的最小ID和最大ID
DECLARE @FirstRec int, @LastRec int
SELECT @FirstRec = (@Page - 1) * @RecsPerPage
SELECT @LastRec = (@Page * @RecsPerPage + 1)

-- 得到實際 的記錄,並返回是否還有數據!
SELECT *,
MoreRecords =
 (
SELECT COUNT(*)
FROM #TempItems TI
WHERE TI.ID >= @LastRec
 )
FROM #TempItems
WHERE ID > @FirstRec AND ID < @LastRec

-- 恢復設置
SET NOCOUNT OFF

在這個存儲過程裏,我們首先創建一個全部 記錄的臨時表,並增加了一個自動編號的字段ID。這樣,不同的記錄就有一個遞增的唯一標誌。
根據當前的頁號和每頁的記錄數,可以計算 出每頁的最小和最大的ID。從而得到當前頁的所有記錄。
爲了顯示的方便,存儲過程還計算了 MoreRecords字段,作爲顯示下一頁的判斷條件。
利用了這個存儲過程的程序代碼如下:
 <%
'每頁顯示10條
Const iRecordsPerPage = 10

Dim currentPage '當前頁號
Dim bolLastPage '在最後一頁?

 if len(Request.QueryString("page")) = 0 then
currentPage = 1
 else
currentPage = CInt(Request.QueryString("page"))
 end if

'得到當前頁的記錄
strSQL = "sp_PagedItems " & currentPage & "," & iRecordsPerPage
objRS.Open strSQL, objConn

'判斷是否在最後一頁
 if Not objRS.EOF then
if CInt(objRS("MoreRecords")) > 0 then
  bolLastPage = False
else
  bolLastPage = True
end if
end if
%>
<P>

 <TABLE BORDER=0 CELLSPACING=1 CELLPADDING=4 ALIGN=CENTER>
<TR><TH COLSPAN=2 BGCOLOR=NAVY>
<FONT SIZE=+1 COLOR=WHITE>
  List of Items
</FONT>
</TH></TR>
 <%
Do While Not objRS.EOF %>
<TR><TD ALIGN=LEFT BGCOLOR=GRAY>
<%=objRS("Name")%>
</TD><TD ALIGN=CENTER BGCOLOR=GRAY>
<%=FormatCurrency(objRS("Price"))%>
</TD></TR>
<% objRS.MoveNext
Loop %>
 </TABLE>
<P>
<CENTER>
 <%

'第一頁不 顯示“前一頁”
 if currentPage > 1 then %>
<INPUT TYPE=BUTTON VALUE="<< 前 <%=iMaxRecords%> 記錄 "
ONCLICK="document.location.href='thispage.asp?page=<%=currentPage-1%>'"> ;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <% end if

'最後一頁 不顯示“後一頁”
 if Not bolLastPage then %>
<INPUT TYPE=BUTTON VALUE="後 <%=iMaxRecords%> 記錄 >>"
ONCLICK="document.location.href='thispage.asp?page=<%=currentPage+1%>'"> ;
 <% end if %>
</CENTER>


 

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