Cocoon _ custom class_paging optimization code

Source: Internet
Author: User
This paging class uses not the most primitive cursor paging method, so the efficiency is much higher than the traditional paging method, and you may be useful.

<Script language = VBScript runat = Server>

'// ---- Cocoon _ custom class_paging optimization code ----//'
Class cc_db_pager
'--------------------------------
'Cocoon db_pager class (Ver: 1)
By sunrise_chen (sunrise_chen@msn.com)
'Please keep this information. Thank you.
'Thanks for eway365 from club.pchome.net
'2017
'
'The Code was greatly modified and some features were added
'Added the distinct selection attribute
'--------------------------------

'// -------------------- Define the variable --------------------//'
Private stablename '// table name
Private ssqlstring '// custom SQL statement
Private acondition () '// query condition (array)
Private scondition '// query condition (string)
Private ipage '// current page number
Private ipagesize '// number of records per page
Private ipagecount '// total number of pages
Private ireccount '// number of records under the current query Condition
Private itotalreccount '// total number of records
Private sfields // name of the output field
Private sorderby '// sorting string
Private ssql '// The current query statement
Private spkey' // primary key
Private oconn' // connection object
Private idefpagesize '// default number of records displayed per page
Private sprojectname '/Project Name
Private sversion'/version number
Private bshowerror' // whether the error message is displayed
Private bdistionct '// whether to display unique records
Private spageinfo '// number of records, page number, and other information
Private spagerfuncname '// frontend paging function name
Private bdistinct '// whether it is distinct
Private spageparam '// page parameter name

'// -------------------- Event and method --------------------//'
'// Class initialization event
Private sub class_initialize ()
Redim acondition (-1)
Sprojectname = "Cocoon series database page turning optimization class"
Sversion = "1.05"
Spkey = "ID"
Sfields = "*"
Scondition = ""
Sorderby = ""
Ssqlstring = ""
Ipagesize = 10
Ipage = 1
Ireccount = NULL
Itotalreccount = NULL
Ipagecount = NULL
Bshowerror = true
Bdistionct = false
Spageinfo = "currently % 2 pages total % 3 records on Page % 1"
Spagerfuncname = "_ cc_dopage"
Spageparam = "page"
End sub

'// Class end event
Private sub class_terminate ()
Set oconn = nothing
End sub

'// I have never understood that there is no IIF () function in VBScript !!!
Private function IIF (expr, val1, val2)
If (expr) then
IIF = val1
Else
IIF = val2
End if
End Function

'// Handle error messages
Public sub doerror (s)
Dim stmp
Stmp = clng (RND () * 100)
Response. Write ("<Div style = 'width: 760; font-size: 9pt; cursor: hand'> ")
Response. Write ("<label onclick = 'errordiv" & stmp & ". style. Display = (errordiv" & stmp & ". style. Display = """"? "" NONE "": ") '> <span style = 'background-color: # cccc00; color: white; '> <cc_db_pager prompt information </span> <br> </label> ")
Response. write ("<Div id = 'errordiv" & stmp & "'style = 'display: none; width: 100%; Border: 1px solid # cccc00; padding: 5; overflow: hidden; text-overflow: ellipsis; '> <nobr> ")
Response. Write ("<span style = 'color: red'> description </span>:" & S & "<br> ")
Response. write ("<span style = 'color: red'> provider </span>:" & sprojectname & "<span style = 'color: red'> version </span>: "& sversion &" <br> ")
Response. Write ("</nobr> </div> <br> ")
End sub

'// SQL statement for paging generation
Public Function getsql ()
Dim istart, iend
Call makecondition ()
If not isnumeric (ipage) Then ipage = 1
If clng (ipage) <1 then ipage = 1
Istart = (ipage-1) * ipagesize
Iend = istart + ipagesize
Getsql = "select" & IIF (bdistinct, "distinct", "") & "& sfields &" from ["& stablename &"] "_
& "Where [" & spkey & "] in ("_
& "Select top" & iend & "[" & spkey & "] from [" & stablename & "]" & scondition & "" & sorderby &""_
&")"
If clng (ipage)> 1 then
Getsql = getsql & "and [" & spkey & "] Not in ("_
& "Select top" & istart & "[" & spkey & "] from [" & stablename & "]" & scondition & "" & sorderby &""_
&")"

End if
Getsql = getsql & "" & sorderby &""
End Function

'// Generate a condition string
Private sub makecondition ()
If Len (scondition)> 0 Then exit sub
If ubound (acondition)> = 0 then
Scondition = "where" & join (acondition, "and ")
End if
End sub

'// Calculate the number of records
Private sub caculatereccount ()
On Error resume next
Dim ors
Call makecondition ()
Ssqlstring = "select count ([" & spkey & "]) from [" & stablename & "]" & IIF (LEN (scondition) <1, "", scondition)
Set ors = oconn. Execute (ssqlstring)
If err then doerror err. Description: Response. End ()
Ireccount = ors. Fields. Item (0). Value
Set ors = nothing
End sub

'// Calculates the total number of records
Private sub caculatetotalreccount ()
Dim ors
Set ors = oconn. Execute ("select count (*) from [" & stablename & "]")
Itotalreccount = ors. Fields. Item (0). Value
Set ors = nothing
End sub

'// Calculate the page number
Private sub caculatepagecount ()
If isnull (ireccount) Then caculatereccount ()
If ireccount = 0 then ipagecount = 0: Exit sub
Ipagecount = ABS (INT (0-(ireccount/ipagesize )))
End sub

'// Set the page number
Private function setpage (N)
Ipage = N
If not isnumeric (ipage) Then ipage = 1
If clng (ipage) <1 then ipage = 1
End Function

'// Add conditions
Public sub addcondition (s)
If Len (s) <0 Then exit sub
Redim preserve acondition (ubound (acondition) + 1)
Acondition (ubound (acondition) = s
End sub

'// Version information
Public Function information ()
Doerror "coding by <a href = 'mailto: sunrise_chen@msn.com '> sunrise_chen </a> @ <a href = 'HTTP: // www.ccopus.com'>; http://www.ccopus.com <;/A>."
End Function

'// -------------------- INPUT attribute --------------------//'
'// Define the connection object
Public property set activeconnection (o)
Set oconn = o
End Property

'// Connection string
Public property let connectionstring (s)
Set oconn = server. Createobject ("ADODB. Connection ")
Oconn. connectionstring = s
Oconn. open ()
End Property

'// Define the query table name
Public property let tablename (s)
Stablename = s
End Property

'// Define the field name to be output
Public property let fields (s)
Sfields = s
End Property

'// Define the primary key
Public property let pkey (s)
Spkey = s
End Property

'// Define the sorting rule
Public property let orderby (s)
Sorderby = "order by" & S &""
End Property

'// Defines the number of records per page
Public property let pagesize (s)
Ipagesize = s
If not isnumeric (ipagesize) Then ipagesize = idefaultpagesize
If clng (ipagesize) <1 then ipagesize = idefaultpagesize
End Property

'// Defines the current page number
Public property let page (s)
Setpage s
End Property

'// Defines the current page number (same as the property page)
Public property let absolutepage (s)
Setpage s
End Property

'// Custom query statement
Public property let SQL (s)
Ssqlstring = s
End Property

'// Whether it is distinct
Public property let distinct (B)
Bdistinct = B
End Property

Public property let pageparam (s)
Spageparam = lcase (s)
End Property

'// -------------------- Output attribute --------------------//'
'// Name of the output query table
Public property get tablename ()
Tablename = stablename
End Property

'// Name of the field to be output
Public property get fields ()
Fields = sfields
End Property

'// Output primary key
Public property get pkey ()
Pkey = spkey
End Property

'// Output the sorting rule
Public property get orderby ()
Orderby = sorderby
End Property

'// Obtain the number of records under the current condition
Public property get recordcount
If isnull (ireccount) Then caculatereccount ()
Recordcount = ireccount
End Property

'// Obtain the number of records per page
Public property get pagesize
Pagesize = ipagesize
End Property

'// Obtain the current query Condition
Public property get Condition
If Len (scondition) <1 then makecondition ()
Condition = scondition
End Property

'// Obtain the current page number
Public property get page
Page = ipage
End Property

'// Obtain the current page number
Public property get absolutepage
Absolutepage = ipage
End Property

'// Obtain the total number of records
Public property get totalrecordcount
If isnull (itotalreccount) Then caculatetotalreccount ()
Totalrecordcount = itotalreccount
End Property

'// Obtain the total number of pages
Public property get pagecount
If isnull (ipagecount) Then caculatepagecount ()
Pagecount = ipagecount
End Property

'// Obtain the record set after pagination
Public property get recordset
On Error resume next
Ssql = getsql ()
Set recordset = oconn. Execute (ssql)
If err then
If bshowerror then doerror err. Description
If Len (ssqlstring)> 0 then
Set recordset = oconn. Execute (ssqlstring)
If err then
Doerror err. Description
Response. End ()
End if
Else
Doerror err. Description
End if
End if
Err. Clear ()
End Property

'// Version information
Public property get version
Version = sversion
End Property

'// Output the front-end script for paging
Public property get pagerscript
Dim Surl, squerystring, X
Surl = request. servervariables ("url ")
Squerystring = ""
For each X in request. querystring
If lcase (x) <> spageparam then squerystring = squerystring & X & "=" & request. querystring (x )&"&"
Next
Pagerscript = vbcrlf &_
"<S" & "cript Language =" "JavaScript" "id =" "cc_script_dbpager" ">" & vbcrlf &_
"//" & Sprojectname & "(Ver:" & sversion & ")" & vbcrlf &_
"// Coding by sunrise_chen (sunrise_chen@msn.com)" & vbcrlf &_
"Function" & spagerfuncname & "(n) {" & vbcrlf &_
"Location. href = '" & Surl &"? "& Squerystring & spageparam &" = '+ N +' "&" '; "& vbcrlf &_
"}" & Vbcrlf &_
"</S" & "strong>" & vbcrlf &_
Vbcrlf
End Property

'// Output page number, number of records, and other information
Public property get pageinfo
Caculatepagecount ()
Pageinfo = Replace (replace (spageinfo, "% 3", ireccount), "% 2", ipagecount), "% 1", ipage)
End Property

'// Output page flip button
Public property get Pager
Pager = "" & vbcrlf _
& "[<A" & IIF (clng (ipage) <= 1, "disabled href = 'javascript: void (0); '", "href = 'javascript: "& spagerfuncname &" (1); '") &"> homepage </a>] "& vbcrlf _
& "[<A" & IIF (clng (ipage) <= 1, "disabled href = 'javascript: void (0); '", "href = 'javascript: "& spagerfuncname &" ("& (iPage-1) &"); '") &"> previous page </a>] "& vbcrlf _
& "[<A" & IIF (clng (ipage)> = clng (ipagecount), "disabled href = 'javascript: void (0 );'", "href = 'javascript:" & spagerfuncname & "(" & (ipage + 1) & "); '") & "> next page </a>]" & vbcrlf _
& "[<A" & IIF (clng (ipage)> = clng (ipagecount), "disabled href = 'javascript: void (0 );'", "href = 'javascript:" & spagerfuncname & "(" & ipagecount & "); '") & "> last page </a>]"
End Property

'// Output the connection statement
Public property get connectionstring ()
Connectionstring = oconn. connectionstring
End Property

'// Output the connection object
Public property get conn ()
Set conn = oconn
End Property

End Class

</SCRIPT>

Example:

Rem includes or copies the above Code to this page.

Dbpath = "db_cc_counter6.mdb" 'defines the Database Name
Dbpassword = ""
Sconnstring = "DBQ =" & server. mappath (dbpath) & "; defaultdir =; driver = {Microsoft Access Driver (*. mdb)};" 'define a connection

Set db_pager = new cc_db_pager create an object in the reference class
Db_pager.connectionstring = sconnstring 'connect to the database
Db_pager.tablename = "t_visit" 'sets the name of the table to be queried.
Db_pager. SQL = "select * From t_visit" 'custom SQL statement

Db_pager.page = request ("page ")
Temstr = temstr & db_pager.pagerscript
Temstr = temstr & db_pager.pageinfo
Temstr = temstr & db_pager.pager
Set myrs = db_pager.recordset
If not (myrs. EOF and myrs. bof) then
Do while not myrs. EOF
Response. write myrs ("ID") & "|" & myrs ("createtime") & "|" & myrs ("lastacttime") & "|" & myrs ("isonline ") & "<br>"
Myrs. movenext
Loop
End if
Response. Write (temstr)
Set db_pager = nothing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.