Pagination recently has been studying how to write a primary page of the paging algorithm, probably sorted out, the idea is as follows:
First you need an AutoNumber field (ID) in the database. Then on the first visit, take out all the records, customize the number of records per page pagesize, calculate the pages, and then build a one-dimensional array based on the pages PageID (PageCount), PageID (0) to save the initial conditions, Each element is then saved with the corresponding ID boundary code for each page
(
1,id Boundary Code: If the database record ID record sequence is as follows 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
assumes the need to sort by id order, PageSize = 5, PageCount = 4, PageID (4)
The values of the
array PageID are PageID (0) = 1, PageID (1) = 5, PageID (2) = 10,pageid (3) =, PageID (4) = 16
when I page to visit the [PageID (I-1), PageID (i)) between the records, so as to ensure that each take the record is only the pagesize record.
assume that you need to sort by ID reverse order,
The values of the
array PageID are PageID (0) =, PageID (1) = one, PageID (2) = 7, PageID (3) = 2, PageID (4) = 1, when accessing page I directly look for IDs belonging to [Pa Geid (i-1), PageID (i))
)
the array PageID () in application () for access so that the application () is initialized the first time a paging program is accessed. The code section is as follows: (hereinafter called a new program)
<%
Time1 = Timer ()
Dim Conn
Set Conn = Server.CreateObject ("adodb.connection")
Conn.Open "Driver={microsoft Access Driver (*.mdb)};D bq=" &server.mappath ("Db.mdb")
Dim page,pagecounts,pageid,pagelist
Dim Rs,sql
Dim isinit,i
isinit = False ' flag to determine whether the application ("PageID") is initialized
pagelist = 20 ' Sets display 20 data per page
Set Rs = Server.CreateObject ("Adodb.recordset")
page = request.querystring ("page") ' Note that the page number needs to be checked for type
If IsEmpty (Application ("PageID")) Then ' If Application (' PageID ') has not been initialized, initialize first
Response.Write ("Init app!<br>")
SQL = "Select * from Test" Desc "' assumes that this is sorted by ID reverse order
rs.open sql,conn,1,1 ' gets recordset object
If not (rs.eof or RS.BOF) Then
rs.pagesize = PageList ' Sets the number of records per page
pagecounts = Rs.pagecount
ReDim PageID (pagecounts) ' Redefine array PageID
for i = 0 to pagecounts ' starts assigning value to array PageID ()
If rs.eof Then Exit for
PageID (i) = Rs ("ID")
Rs.move (pagelist)
Next
Rs.movelast
PageID (pagecounts) = Rs ("ID")
Application.Lock ()
application ("PageID") = PageID
Application.UnLock ()
End If
Rs.close
End If
Idstart = Clng (Application ("PageID") (Page-1))
idend = Clng (Application ("PageID") (Page))
SQL = "SELECT * FROM Test where id<=" &IdStart& "and id>" &IdEnd& ""
Rs.Open sql,conn,1,1
While not rs.eof
Response.Write (RS (0) & "--" &rs (1))
Rs.movenext
Wend
Rs.close
Set Rs = Nothing
Conn.close
Set Conn = Nothing
for i = 1 to Ubound (Application ("PageID"))
Response.Write ("<a href= ' test1.asp?") Page= "&i&" ' > "&i&" </a> ")
Next
Time2 = Timer ()
Response.Write ("<br>" & (time2-time1) *1000)
' Application.Contents.Remove ("PageID")
%>
traditional paging code is as follows: (hereinafter referred to as the old program)
<%
Time1 = Timer ()
Dim Conn
Set Conn = Server.CreateObject ("adodb.connection")
Conn.Open "Driver={microsoft Access Driver (*.mdb)};D bq=" &server.mappath ("Db.mdb")
Dim page,pagecounts,pagelist
Dim Rs,sql
pagelist = 20
page = request.querystring ("page")
Set Rs = Server.CreateObject ("Adodb.recordset")
SQL = "SELECT * from test ORDER BY id DESC"
Rs.Open sql,conn,1,1
If page = "" Then page = 1
If not (rs.eof Or rs.bof) Then
rs.pagesize = PageList
pagecounts = Rs.pagecount
rs.absolutepage = Page
End If
for i = 1 to pagelist
If rs.eof Then Exit for
Response.Write (Rs (0) & "-----" &rs (1) & "<br>")
Rs.movenext
Next
for i = 1 to pagecounts
Response.Write ("<a href= ' test.asp?") Page= "&i&" ' > "&i&" </a> ")
Next
Time2 = Timer ()
Response.Write ("<br>" & (time2-time1) *1000)
%>
In fact, the overall idea is to create a application ("PageID") global array, where each element saves the ID interval of the page's area record, for example, application ("PageID") (0) Saves the ID of the first element, Then application ("PageID") (1) Saves the first ID of the next page ... And so on, when you need to access page I, look directly at the recordset in [Application ("PageID") (i-1), Application ("I"), so that you can only find the number of records you need each time, instead of