Phân tran trong ngôn ngữ lập trình ASP.
<%@LANGUAGE=VBScript%>
<%
‘ Define variables
dim recordsonpage, requestrecords, offset, allrecords, hiddenrecords, showrecords, lastrecord, recordcounter, pagelist, pagelistcounter
‘ DB connection
dim Conn
Set Conn = Server.CreateObject(”ADODB.Connection”)
sConnection = “Provider=Microsoft.Jet.OLEDB.4.0;” & _
“Data Source=” & Server.MapPath(”cddatabase.mdb”) & “;” & _
“Persist Security Info=True”
Conn.Open(sConnection)
‘ records per page
recordsonpage = 10
‘ count all records
allrecords = 0
set rs = conn.Execute(”SELECT * FROM cds”)
do until rs.EOF
allrecords = allrecords + 1
rs.movenext
loop
‘ if offset is zero then the first page will be loaded
offset = request.querystring(”offset”)
if offset = 0 OR offset = “” then
requestrecords = 0
else
requestrecords = requestrecords + offset
end if
‘ opens database
set rs = conn.Execute(”SELECT * FROM cds ORDER BY cdid”)
‘ reads first records (offset) without showing them (can’t find another solution!)
hiddenrecords = requestrecords
do until hiddenrecords = 0 OR rs.EOF
hiddenrecords = hiddenrecords – 1
rs.movenext
if rs.EOF then
lastrecord = 1
end if
loop
%>
<%
‘ prints records in the table
showrecords = recordsonpage
recordcounter = requestrecords
do until showrecords = 0 OR rs.EOF
recordcounter = recordcounter + 1
%>
<%
showrecords = showrecords – 1
rs.movenext
if rs.EOF then
lastrecord = 1
end if
loop
%>
<% = recordcounter %> <% = rs(”cdid”) %> <% = rs(”title”) %>
<% if requestrecords <> 0 then %>Prev Page<% else %>Prev Page<% end if %> <% if lastrecord <> 1 then %> Next Page<% else %>Next Page<% end if %>
pagelist:
<%
pagelist = 0
pagelistcounter = 0
do until pagelist > allrecords
pagelistcounter = pagelistcounter + 1
%>
<% = pagelistcounter %>
<%
pagelist = pagelist + recordsonpage
loop
%>
<%
‘ Closes connection
rs.close
Conn.close
%>
Các bạn có thể download code hoàn chỉnh tại đây http://www.encysoft.com/download/paging.txt