<%
'******************************
' Name: Pagination class
' Date: 2005/12/3
' Author: West Building Cold Month
' URL: www.xilou.net | www.chinaCMS.org
' Description: None
' Copyright: Reprint, please note the name of the source, the author
'******************************
Class Page
Private Currpage
Private Pagen
Private Urlstr
Private TempStr
Private Errinfo
Private Iserr
Private Totalrecord
Private Totalpage
Public pagers
Private Tempa (11)
Private TEMPB (8)
'------------------------------------------------------------
Private Sub Class_Initialize ()
Currpage=1 '///default display current page as first page
pagen=10 '//default display 10 data per page
Urlstr= "#"
Tempstr= ""
errinfo= "Errinfo:"
Iserr=false
End Sub
Private Sub Class_Terminate ()
If IsObject (pagers) Then
Pagers.close
Set pagers=nothing
End If
Erase Tempa
Erase TEMPB
End Sub
'----------------------------------------------------------
'//Get current page number
Public Property Let CurrentPage (Val)
Currpage=val
End Property
Public Property Get CurrentPage ()
Currentpage=currpage
End Property
'//Get the number of display bars per page
Public Property Let Pagenum (Val)
Pagen=val
End Property
Public Property Get Pagenum ()
Pagenum=pagen
End Property
'//Get URL
Public Property Let Url (Val)
Urlstr=val
End Property
Public Property Get Url ()
Url=urlstr
End Property
'//Get template
Public Property Let Temp (Val)
Tempstr=val
End Property
Public Property Get Temp ()
Temp=tempstr
End Property
'------------------------------------------------------------
Public Sub Exec (sql,connobj)
On Error Resume Next
Set pagers=server.createobject ("ADODB. RecordSet ")
Pagers.cursorlocation = 3 ' uses client-side cursors to improve efficiency
Pagers.pagesize = Pagen ' defines the number of records per page for a paging recordset
Pagers.open sql,connobj,0,1
If err.number<>0 Then
Err.Clear
Pagers.close
Set pagers=nothing
errinfo=errinfo& "set or open Recordset error ..."
Iserr=true
Response.Write Errinfo
Response.End
End If
Totalrecord=pagers.recordcount '//What if for 0?
If totalrecord>=1 Then
'----------------------------------------------------------------------------start
'//Calculate the total number of pages, Ps, why not pagers.pagecount it?
' If totalrecord Mod pagen=0 Then
' Totalpage=pagers.recordcount\pagen
' Else
' Totalpage=pagers.recordcount\pagen
' Totalpage=abs (Int (totalpage))
' End If
Totalpage=pagers.pagecount
'//Process the currently received page number, the default is 1, so it's not a numeric type that will be 1.
If IsNumeric (currpage) Then
CURRPAGE=CLNG (Currpage)
If currpage<1 Then currpage=1
If currpage>totalpage Then Currpage=totalpage
Else
'//dim m:m= ': IsNumeric (M) =true
Currpage=1
End If
'---------------------------------------------------------------------------End
Else
Totalpage=0
Currpage=1
End If
'//
Pagers.absolutepage = Currpage ' AbsolutePage: Set pointer to start of a page
Pagers.pagesize=pagen
End Sub
Private Sub Init ()
' Private Tempa (10)
Tempa (1) = "{N1}"/Home
Tempa (2) = "{N2}" '//Prev
Tempa (3) = "{N3}" '//Next page
Tempa (4) = "{N4}"/End
Tempa (5) = ' {N5} '//Current page number
Tempa (6) = ' {N6} '/Total page number
Tempa (7) = "{N7}" '//per page number
Tempa (8) = "{N8}"/Total number of articles
Tempa (9) = "{L}"//Loop label start
Tempa (Ten) = "{N}"//Circular Single label: page number
Tempa (one) = "{L/}"//Loop label end
' Private TEMPB (8)
TEMPB (1) = "Home"
TEMPB (2) = "Prev"
TEMPB (3) = "Next Page"
TEMPB (4) = "Last"
TEMPB (5) =currpage '//Current page
TEMPB (6) =totalpage '//page total
TEMPB (7) =pagen '//per page number
TEMPB (8) =totalrecord '//total number of articles
End Sub
Public Sub Show (Style)
If iserr=true Then
Response.Write Errinfo
Exit Sub
End If
Call Init ()
Select Case Style
Case 1
Response.Write Stylea ()
Case 2
Response.Write Styleb ()
Case 3
Response.Write Stylec ()
Case 4
Response.Write styled ()
Case Else
errinfo=errinfo& "No current style ..."
Response.Write Errinfo
End Select
End Sub
Public Function showstyle (Style)
If iserr=true Then
Showstyle=errinfo
Exit Function
End If
Call Init ()
Select Case Style
Case 1
Showstyle= Stylea ()
Case 2
Showstyle= Styleb ()
Case Else
errinfo=errinfo& "No current style ..."
Showstyle=errinfo
End Select
End Function
Private Function Stylea ()
' Home previous next last page is 1th 20 page, total 20 pages, each page 10, article total 200
'//Pagination sample: [Home] [prev] [Next page] [last page] [page: 4/5] [Total 86 pages 20/page] Go to: _ Page
'//Tag: {N1} {N2} {N3} {N4} total: {N8} record {N6} page is currently {N5} page {N7} per page
If IsEmpty (TEMPSTR) Then
errinfo=errinfo& "Template is empty ..."
Styleb=errinfo
Exit Function
End If
Dim M
If totalpage>1 Then
If currpage>1 Then
m= "<a href= '" &UrlStr& "Page=1 ' >" & "Home" & "</a>"
Tempstr=replace (TempStr, "{N1}", M)
m= "<a href= '" &UrlStr& "page=" &CurrPage-1& "' >" & "Prev" & "</a>"
Tempstr=replace (TempStr, "{N2}", M)
If Currpage<totalpage Then
m= "<a href= '" &UrlStr& "page=" &CurrPage+1& "' >" & "Next" & "</a>"
Tempstr=replace (TempStr, "{N3}", M)
m= "<a href= '" &UrlStr& "page=" &TotalPage& "' >" & "Last" & "</a>"
Tempstr=replace (TempStr, "{N4}", M)
Else
Tempstr=replace (TempStr, "{N3}", "Next Page")
Tempstr=replace (TempStr, "{N4}", "Last")
End If
Else
Tempstr=replace (TempStr, "{N1}", "Home")
Tempstr=replace (TempStr, "{N2}", "Prev")
m= "<a href= '" &UrlStr& "page=" &CurrPage+1& "' >" & "Next" & "</a>"
Tempstr=replace (TempStr, "{N3}", M)
m= "<a href= '" &UrlStr& "page=" &TotalPage& "' >" & "Last" & "</a>"
Tempstr=replace (TempStr, "{N4}", M)
End If
Else
Tempstr=replace (TempStr, "{N1}", "Home")
Tempstr=replace (TempStr, "{N2}", "Prev")
Tempstr=replace (TempStr, "{N3}", "Next Page")
Tempstr=replace (TempStr, "{N4}", "Last")
End If
T=tempstr
T=replace (T, "{N8}", Totalrecord)
T=replace (T, "{N6}", Totalpage)
T=replace (T, "{N5}", Currpage)
T=replace (T, "{N7}", Pagen)
Tempstr=t
Stylea=tempstr
End Function
Private Function Styleb ()
' Home |< 1 2 3 4 5 6 7 >| Last
'//Tag: {N1} {N2} {L}{N}{L/}{N3}{N4}
If IsEmpty (TEMPSTR) Then
errinfo=errinfo& "Template is empty ..."
Styleb=errinfo
Exit Function
End If
Dim forcenum,backnum '//Current page front and rear display number
Forcenum=5
Backnum=4
Dim M
'//Home
m= "<a href= '" &UrlStr& "page=1 ' >" &TEMPB (1) & "</a>"
Tempstr=replace (TempStr, "{N1}", M)
'//Last
m= "<a href= '" &UrlStr& "page=" &TEMPB (6) & "' >" &TEMPB (4) & "</a>"
Tempstr=replace (TempStr, "{N4}", M)
'//Previous page
M= "|<"
If currpage-1>=1 Then
m= "<a href= '" &UrlStr& "page=" &CurrPage-1& ">" & "|<" & "</a>"
End If
Tempstr=replace (TempStr, "{N2}", M)
'//Next page
M= ">|"
If Currpage+1<=totalpage Then
m= "<a href= '" &UrlStr& "page=" &CurrPage+1& ">" & ">|" & "</a>"
End If
Tempstr=replace (TempStr, "{N3}", M)
'//Remove cyclic label
Dim n1,n2,n3,n4,n5,n6
If InStr (TempStr, "{L}") >0 Then
N1=instr (TempStr, "{L}")
End If
If InStr (TempStr, "{L/}") >0 Then
N2=instr (TempStr, "{L/}")
End If
If n2<=n1 Then
errinfo=errinfo& "Loop label error ..."
Styleb=errinfo
Exit Function
End If
N3=mid (tempstr,n1,n2-n1+4) '//store template including {l}{l/} cyclic label
N4=replace (N3, "{L}", "") '//store templates that do not include the {l}{l/} loop label
N4=replace (N4, "{L/}", "")
'//Page number list
Dim Firstpagenum,lastpagenum
If currpage-forcenum<=1 Then
Firstpagenum=1
Pagelist= ""
Else
Firstpagenum=currpage-forcenum
Pagelist= "..."
End If
If Currpage+backnum>=totalpage Then
Lastpagenum=totalpage
Pagelist_2= ""
Else
Lastpagenum=currpage+backnum
Pagelist_2= "..."
End If
Dim I
For I=firstpagenum to Lastpagenum
If I=currpage Then
N5=replace (N4, "{N}", "<b>" &I& "</b>")
N6=n6&n5
Else
m= "<a href= '" &UrlStr& "page=" &I& ">" &I& "</a>"
N5=replace (N4, "{N}", M)
N6=n6&n5
End If
Next
Tempstr=replace (TEMPSTR,N3,N6)
Styleb=tempstr
End Function
Private Function Stylec ()
' Home |< |<< 1 2 3 4 5 6 7 >>| >| Last
'//This style is modified on the basis of Styleb, add two tags: {N9} 10 page {N10} next 10 page
'//Tag: {N1}{N2}{N9}{L}{N}{L/}{N10}{N3}{N4}
Dim T
T=styleb ()
'//First 10 page
M= "|<<"
If currpage-10>=1 Then
m= "<a href= '" &UrlStr& "page=" &CurrPage-10& ">" & "|<<" & "</a>"
End If
T=replace (T, "{N9}", M)
M= ">>|"
If Currpage+10<=totalpage Then
m= "<a href= '" &UrlStr& "page=" &CurrPage+10& ">" & ">>|" & "</a>"
End If
T=replace (T, "{N10}", M)
Stylec=t
End Function
Private Function styled ()
'//This style is modified on the basis of Stylec
'//Total {N8} record {N6} page is currently {N5} page {N7} per page
'//Home |< |<< 1 2 3 4 5 6 7 >>| >| Last
'//Tag: {N1}{N2}{N9}{L}{N}{L/}{N10}{N3}{N4}
Dim T
T=stylec ()
T=replace (T, "{N8}", Totalrecord)
T=replace (T, "{N6}", Totalpage)
T=replace (T, "{N5}", Currpage)
T=replace (T, "{N7}", Pagen)
Styled=t
End Function
End Class
%>
Call Example:
Program code
Set test = New Page
test.CurrentPage=request.QueryString("page") '自己去读取拉
test.temp="{N1} {N2} {L} {N} {L/} {N3} {N4}"
test.Exec "Select * From tblname",conn
test.Show 4 '调用第四种风格显示分页
test.PageNum = 5