ASP Paging List Code
Last Update:2017-01-13
Source: Internet
Author: User
Call Method:
Dim Mylistpager
Set Mylistpager = new Listpager
With Mylistpager
. Connection = conn
. Table = "Photos"
. PrimaryKey = "ID"
. Fields = "Photourl,title,introduce"
. Init ()
Dim arrlist:arrlist =. GetRows ()
For i=0 to Ubound (arrlist,2)
Response.Write ("<div><img src=" "upload/" &arrlist (0,i) & ""/></div> ")
Next
. Drawpaging 8, "", NULL
End With
Set Mylistpager = Nothing
Pagination Code:
<%
'----------------------------------------------------------
Class Listpager
Private M_dbtype, M_connection, m_table, M_primarykey, M_fields, M_commandtext
Private M_cache, M_expires, M_recordcount, M_pagesize, M_pagecount, M_currentpage
Private M_where, M_whereand, M_orders (1,4), M_ordubound, m_querystring
Private M_order, M_orderasc, M_orderdesc, M_prefields, M_prerecordcount
Public Property Get Version
Version = "Fonshen ASP listpager Class Version 1.02.08.02.17"
End Property
Public Property Let DbType (Strdbtype)
M_dbtype = UCase (DbType)
End Property
Public Property Let Connection (Objconnection)
Set m_connection = objconnection
End Property
Public Property Let Table (Strtable)
M_table = strtable
End Property
Public Property Let PrimaryKey (Strprimarykey)
M_primarykey = Lcase (Strprimarykey)
End Property
Public Property Let Fields (Strfields)
M_fields = Strfields
End Property
Public Property Let Cache (Intcache)
M_cache = Intcache
End Property
Public Property Let RecordCount (Intrecordcount)
M_recordcount = Intrecordcount
End Property
Public Property Get RecordCount
RecordCount = M_recordcount
End Property
Public Property Let PageSize (Intpagesize)
M_pagesize = Intpagesize
End Property
Public Property Get PageSize
PageSize = M_pagesize
End Property
Public Property Get PageCount
PageCount = M_pagecount
End Property
Public Property Get CurrentPage
CurrentPage = M_currentpage
End Property
Public Property Get QueryString
QueryString = M_querystring
End Property
Public Property Get CommandText
CommandText = M_commandtext
End Property
Public Sub Addwhere (strwhere)
If M_where = "" Then
M_where = "(" & Strwhere & ")"
Else
M_where = M_where & "and (" & Strwhere & ")"
End If
End Sub
Public Sub AddOrder (Strfield,strorder)
M_ordubound = M_ordubound + 1
If M_ordubound>ubound (m_orders,2) then ReDim m_orders (1,2*m_ordubound-1)
M_orders (0,m_ordubound) = Lcase (Trim (Strfield))
M_orders (1,m_ordubound) = Ucase (Trim (Strorder))
End Sub
Private Function Tolong (str,defautvalue)
If IsNumeric (str) Then
Tolong = CLNG (str)
Else
Tolong = Defautvalue
End If
End Function
Private Sub Class_Initialize
M_ordubound =-1
M_cache = 0
M_recordcount =-1
M_pagesize = 20
End Sub
Private Sub Class_Terminate
End Sub
Private Sub Init_where ()
If M_where = "" Then
M_whereand = "WHERE"
Else
M_where = "Where" & M_where
M_whereand = M_where & "and"
End If
End Sub
Private Sub Init_order ()
Dim Isnotstrongorder:isnotstrongorder = True
M_prefields = M_primarykey
For I=0 to M_ordubound
If M_orders (1,i) = "DESC" Then
M_ORDERASC = M_ORDERASC & M_orders (0,i) & "DESC"
m_orderdesc= M_orderdesc & M_orders (0,i) & "ASC"
Else
M_ORDERASC = M_ORDERASC & M_orders (0,i) & "ASC"
m_orderdesc= M_orderdesc & M_orders (0,i) & "DESC"
End If
M_order = M_orderasc
If M_orders (0,i) = M_primarykey Then
Isnotstrongorder = False
Exit for
Else
M_ORDERASC = M_ORDERASC & ","
m_orderdesc= m_orderdesc& ","
m_prefields= m_prefields& "," & M_orders (0,i)
End If
Next
If Isnotstrongorder Then
M_ORDERASC = M_ORDERASC & M_primarykey & "ASC"
m_orderdesc= m_orderdesc& M_primarykey & "DESC"
End If
If M_order <> "" Then M_order = "ORDER By" & M_order
M_ORDERASC = "ORDER By" & M_ORDERASC
m_orderdesc= "ORDER BY" & M_orderdesc
End Sub
Private Sub Init_recordcount ()
Dim Item, querystringname:querystringname = "Listpager"
For each Item in Request.QueryString
If StrComp (Item, Querystringname, 1) <>0 Then
m_querystring = m_querystring & Item & "=" & Server.URLEncode (Request.QueryString (item)) & "&"
End If
Next
Dim Queryarray:queryarray = Split (Request.QueryString (Querystringname), ",")
Dim Intnow:intnow = DateDiff ("n", DateSerial (1, 1), now)
M_expires = Intnow
M_currentpage = 1
Select Case Ubound (Queryarray)
Case 0
M_currentpage = Tolong (Queryarray (0), 1)
Case 2
M_currentpage = Tolong (Queryarray (2), 1)
M_expires = Tolong (Queryarray (0), m_expires)
End Select
If M_recordcount < 0 and m_cache > 0 Then
If M_expires > Intnow Then
M_recordcount = Tolong (Queryarray (1),-1)
If M_recordcount =-1 Then m_expires = Intnow + M_cache
Else
M_expires = Intnow + M_cache
End If
Else
M_expires = Intnow
End If
If M_recordcount =-1 Then
M_recordcount = M_connection.execute ("Select COUNT (" & M_primarykey & ") from" & M_table & M_where) (0)
End If
If m_expires = Intnow Then
m_querystring = m_querystring & querystringname & "="
Else
m_querystring = m_querystring & querystringname & "=" & M_expires & "," & M_recordcount & ","
End If
End Sub
Private Sub Init_currentpage ()
Dim i:i = m_recordcount MoD m_pagesize
If i = 0 Then
M_pagecount = M_recordcount/m_pagesize
Else
M_pagecount = (m_recordcount-i)/m_pagesize + 1
End If
If m_currentpage<1 then M_currentpage = 1
If M_currentpage>m_pagecount then M_currentpage = M_pagecount
M_prerecordcount = M_currentpage * m_pagesize
End Sub
Private Sub Init_dbtype ()
Select Case M_dbtype
Case "ACCESS", "MSSQL", "MYSQL", "ORACLE", "Pgsql"
Case Else
Select Case (M_connection.provider)
Case "MSDASQL.1", "Sqloledb.1", "SQLOLEDB": M_dbtype = "MSSQL"
Case "Msdaora.1", "oraoledb.oracle": M_dbtype = "Oracle"
Case "Microsoft.Jet.OLEDB.4.0": M_dbtype = "ACCESS"
End Select
End Select
End Sub
Public Sub Init ()
Call Init_where ()
Call Init_order ()
Call Init_recordcount ()
Call Init_currentpage ()
Call Init_dbtype ()
End Sub
Public Function GetRows ()
Select Case M_dbtype
Case "MSSQL"
If M_currentpage = 1 Then
GetRows = Pospageonegetrows ()
ElseIf m_currentpage = M_pagecount Then
GetRows = Negtoppostopgetrows ()
ElseIf M_ORDERASC = "ORDER By" & M_primarykey & "ASC" Then
If M_currentpage * 2 > M_pagecount Then
GetRows = Negpkcutgetrows ("<", "MIN")
Else
GetRows = Pospkcutgetrows (">", "MAX")
End If
ElseIf M_order = "ORDER By" & M_primarykey & "DESC" Then
If M_currentpage * 2 > M_pagecount Then
GetRows = Negpkcutgetrows (">", "MAX")
Else
GetRows = Pospkcutgetrows ("<", "MIN")
End If
Else
If M_currentpage * 2 > M_pagecount Then
GetRows = Negtoppostopgetrows ()
Else
GetRows = Postopnegtopgetrows ()
End If
End If
Case "MYSQL"
GetRows = Limitgetrows ()
Case Else
GetRows = Absolutepositiongetrows ()
End Select
End Function
Private Function pospageonegetrows ()
M_commandtext = "SELECT Top" & m_pagesize & "" &m_fields & "from" & M_table & M_where & M_o Rderasc
Pospageonegetrows = M_connection.execute (M_commandtext). GetRows ()
End Function
Private Function negpageonegetrows ()
M_commandtext = "Select" & M_fields & "from" &_
' Select Top ' & (m_recordcount-(m_pagecount-1) *m_pagesize) & "" & M_fields & "from" & M_table & M _where & M_orderdesc&_
") as Derivedtbl_1" & M_ORDERASC
Negpageonegetrows = M_connection.execute (M_commandtext). GetRows ()
End Function
Private Function pospkcutgetrows (Oper, func)
M_commandtext = "SELECT Top" & m_pagesize & "" &m_fields & "from" & M_table & M_whereand &_
M_primarykey & Oper & "(select" & Func & "(" & M_primarykey & ") from (" &_
' Select Top ' & (m_prerecordcount-m_pagesize) & "& M_primarykey &" from "& M_table & M_wher E & M_orderasc &_
") as Derivedtbl_1)" & M_ORDERASC
Pospkcutgetrows = M_connection.execute (M_commandtext). GetRows ()
End Function
Private Function negpkcutgetrows (Oper, func)
M_commandtext = "Select" & M_fields & "from" &_
"SELECT Top" & M_pagesize & "" & M_fields & "from" & M_table & M_whereand &_
M_primarykey & Oper & "(select" & Func & "(" & M_primarykey & ") from (" &_
' Select Top ' & (M_recordcount-m_prerecordcount) & "& M_primarykey &" from "& M_table & M_w Here & M_orderdesc &_
") as Derivedtbl_1)" & M_orderdesc &_
") as derivedtbl_2" & M_ORDERASC
Negpkcutgetrows = M_connection.execute (M_commandtext). GetRows ()
End Function
Private Function postopnegtopgetrows ()
M_commandtext = "Select" & M_fields & "from" & M_table & "WHERE" & M_primarykey & "" ;_
"Select Top" & M_pagesize & "& M_primarykey &" from ("&_
"SELECT Top" & M_prerecordcount & "" & M_prefields & "from" & M_table & M_where & M_ordera SC &_
"A" & M_orderdesc & ")" & M_ORDERASC
Postopnegtopgetrows = M_connection.execute (M_commandtext). GetRows ()
End Function
Private Function negtoppostopgetrows ()
M_commandtext = "Select" & M_fields & "from" & M_table & "WHERE" & M_primarykey & "" ;_
"Select Top" & M_pagesize & "& M_primarykey &" from ("&_
' Select Top ' & (M_recordcount + m_pagesize-m_prerecordcount) & "" & M_prefields & "from" & M_tabl E & m_where & M_orderdesc &_
"A" & M_orderasc & ")" & M_ORDERASC
Negtoppostopgetrows = M_connection.execute (M_commandtext). GetRows ()
End Function
Private Function limitgetrows ()
M_commandtext = "Select" & M_fields & "from" & M_table & M_where & m_orderasc & "LIMIT" & (m_prerecordcount-m_pagesize) & "," & M_pagesize
Limitgetrows = M_connection.execute (M_commandtext). GetRows ()
End Function
Private Function absolutepositiongetrows ()
Dim RS
Set rs = Server.CreateObject ("Adodb.recordset")
M_commandtext = "SELECT Top" & M_prerecordcount & "" & M_fields & "from" & M_table & M_where & Amp M_order
Rs. Open m_commandtext,m_connection,1,1,&h0001
Rs. AbsolutePosition = m_prerecordcount-m_pagesize + 1
Absolutepositiongetrows = Rs. GetRows (M_pagesize)
Rs.close ()
Set rs = Nothing
End Function
Private Function postopnotingetrows ()
M_commandtext = "Select" & M_fields & "from" & M_table & "WHERE" & M_primarykey & "" ;_
"SELECT Top" & M_pagesize & "" & M_primarykey & "from" & M_table & M_whereand & M_primaryk EY & "Not In" ("&_
' Select Top ' & (m_prerecordcount-m_pagesize) & "& M_primarykey &" from "& M_table & M_where & M_orderasc &_
")" & M_orderasc & ")" & M_ORDERASC
Postopnotingetrows = M_connection.execute (M_commandtext). GetRows ()
End Function
Private Function negtopnotingetrows ()
M_commandtext = "Select" & M_fields & "from" & M_table & "WHERE" & M_primarykey & "" ;_
"SELECT Top" & M_pagesize & "" & M_primarykey & "from" & M_table & M_whereand & M_primaryk EY & "Not In" ("&_
' Select Top ' & (M_recordcount-m_prerecordcount) & "& M_primarykey &" from "& M_table & M_WH Ere & M_orderdesc &_
")" & M_orderdesc & ")" & M_ORDERASC
Negtopnotingetrows = M_connection.execute (M_commandtext). GetRows ()
End Function
Public Sub drawpaging (intpages,strclass,arrtext)
Dim chrsep:if strclass= "" Then Chrsep = ""
If IsNull (arrtext) Then arrtext = Array ("total:<a>", "</a> page:<span>", "</span>", "Go")
With Response
. Write (vbcrlf& "<form class=" "&strClass&" "onsubmit=" "Location.replace" ""? &m_QueryString& "' +this. Pagenumber.value); return false ">"
. Write (VBCRLF&CHR (9) & "<div class=" "PageNumbers" ">" &VBCRLF&CHR (9) &CHR (9))
Dim dblpages:dblpages = intpages*2
Dim Intfrom:intfrom = m_currentpage-intpages
If Intfrom < 1 then Intfrom = 1
Dim Intto:intto = Intfrom + dblpages
If Intto > M_pagecount Then
Intto = M_pagecount
If M_pagecount > Dblpages Then
Intfrom = Intto-dblpages
Else
Intfrom = 1
End If
End If
If Intfrom > 1 Then
. Write ("<a href=" ""?) &m_QueryString& "1" ">1</a>" &chrsep)
. Write ("<a href=" ""?) &m_QueryString& (m_currentpage-1) & "" >&lt;</a> "&chrsep)
End If
For i = Intfrom to Intto
If i = M_currentpage Then
. Write ("<span>" &i& "</span>" &chrsep)
Else
. Write ("<a href=" ""?) &m_QueryString&i& "" ">" &i& "</a>" &chrsep)
End If
Next
If Intto < M_pagecount Then
. Write ("<a href=" ""?) &m_QueryString& (m_currentpage+1) & "" >&gt;</a> "&chrsep)
. Write ("<a href=" ""?) &m_QueryString&m_PageCount& "" ">" &m_PageCount& "</a>" &chrsep)
End If
. Write (VBCRLF&CHR (9) & "</div>")
. Write (VBCRLF&CHR (9) & "<div class=" "PageInfo" ">" &VBCRLF&CHR (9) &CHR (9))
. Write (arrtext (0) & M_recordcount & Arrtext (1) & M_pagesize & Arrtext (2) &_
"<input type=" "Text" "Size=3 name=" "PageNumber" "Value=" "" & M_currentpage & ""/> "&_
"<input type=" "Submit" "Value=" "&arrtext (3) &" "/>")
. Write (VBCRLF&CHR (9) & "</div>")
. Write (vbcrlf& "</form>" &vbcrlf)
End With
End Sub
End Class
%>