Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    Senior Coder Morgoth's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada Remaining Brain Cells: 6
    Posts
    1,402
    Thanks
    2
    Thanked 1 Time in 1 Post

    Code: Page Numbers Script

    Hello,

    This is a page number script I wrote along time ago for a screen shot section of a wesbite I managed. I rewrote the script with detailed comments so you can understand why I did what I did to get the script working. I am offering this script to codingforums.com because of the large community of users who will appreciate and contribute to this script. I hope it helps with whatever you use it for.

    PageNumbers.asp:
    Code:
    <%Option Explicit%>
    <%
    'Function designed to check if string contants only numbers
    Function IsOnlyNumeric(str)
       Dim ianRegEx
       Set ianRegEx = New RegExp
       ianRegEx.Pattern = "[^0-9]"
       ianRegEx.Global = True
       IsOnlyNumeric = (ianRegEx.Test(str) = False)
    End Function
    %>
    <%
    'Open database connection (Access)
    Dim oConn, StrConn, SQL, oRS
    Set oConn = Server.CreateObject("ADODB.Connection") 
    StrConn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & Server.MapPath("db\db1.mdb") & ";" 
    oConn.Open StrConn
    
    'Count the number of records in the database
    SQL = "SELECT Count(ID) AS RecordCount FROM tblData"
    Set oRS = oConn.Execute(SQL)
    
    Dim intRecordsPerPage, intNumberOfPages, intRecordCount
    intRecordsPerPage = 5 'Number of records per page
    intRecordCount = oRS("RecordCount") 'Number of records in database
    
    Dim intPageNumber
    'Check to make sure page number in QueryString is valid
    If IsOnlyNumeric(Request.QueryString("page")) = True And Request.QueryString("page") <> "" Then
      intPageNumber = Int(Request.QueryString("page"))
    Else
      intPageNumber = 1
    End If
    
    'Make sure there are records in the database
    If Not oRS.EOF Then
      'Calculate the number of pages based on how many records there are divided by how many records per page
      'By adding and subtracting 0.5 and using CInt() to round, this will calculate the most pages needed for how many records there are
      intNumberOfPages = CInt((intRecordCount - 0.5) / intRecordsPerPage + 0.5) 
    
      'This section creates the hyperlinks for the page numbers in a string to be posted anywhere on the page any number of times
      Dim intPagesAround, intRightPages, intLeftPages
      intPagesAround = 2 'Page number hyperlinks on either side of select page
      'Show default page numbers on either side
      intRightPages = intPagesAround 
      intLeftPages = intPagesAround
    
      'Calculates how many page numbers are shown on either side of the selected page to show a total of intPagesAround * 2 + the selected page
      If intPageNumber <= intPagesAround Then
        intRightPages = intPagesAround * 2 - intPageNumber + 1
      ElseIf intPageNumber >= intNumberOfPages - intPagesAround Then
        intLeftPages = intPagesAround * 2 - intNumberOfPages + intPageNumber
      End If
    
      Dim strPageCount, intPageCount
      'If the page number is greater than 1, then a previous button is added for easy descending
      If intPageNumber > 1 Then
        strPageCount = strPageCount & "<a href=""pagenumbers.asp?page=" & intPageNumber - 1 & """>«</a> "
      Else
        strPageCount = strPageCount & "<span style=""color: #FFFFFF;"">«</span> "
      End If
    
      For intPageCount = 1 To intNumberOfPages
        If intPageCount <> intPageNumber Then
          If intPageCount <= intPageNumber + intRightPages And intPageCount >= intPageNumber - intLeftPages Then
            strPageCount = strPageCount & "<a href=""pagenumbers.asp?page=" & intPageCount & """>" & intPageCount & "</a> "
          End If
        Else
          strPageCount = strPageCount & intPageCount & " "
        End If
      Next
    
      'If the page number is less than the total amount of pages, then a next button is added for easy forwarding
      If intPageNumber < intNumberOfPages Then
        strPageCount = strPageCount & "<a href=""pagenumbers.asp?page=" & intPageNumber + 1 & """>»</a> "
      Else
        strPageCount = strPageCount & "<span style=""color: #FFFFFF;"">«</span> "
      End If
      'strPageCount is the string that contains all the page number hyperlinks
    
    End If
    
    'Grab records from database
    SQL = "SELECT ID, fldInfo FROM tblData ORDER BY ID DESC"
    Set oRS = oConn.Execute(SQL)
    
    'Move cursor to appropriate record depending on page number
    oRS.MoveFirst 'Move cursor to first record
    If intPageNumber <= intNumberOfPages And intPageNumber >= 1 Then 'Check to make sure page number entered is in correct parameters
      oRS.Move((intPageNumber - 1) * intRecordsPerPage) 'Move record cursor to specific position determined by page number given
    ElseIf intPageNumber < 1 Then 'Check to see if the QueryString page number is less than 1
      Response.Redirect("PageNumbers.asp?page=1") 'Reset QueryString due to user fault
      'OR Move first because QueryString page number is less than 1
      'oRS.MoveFirst 'Moves the record cursor to the begining of the records, but does not change user's mistake in the QueryString
    ElseIf intPageNumber > intNumberOfPages Then 'Check to see if the QueryString page number is greater than the number of pages available
      Response.Redirect("PageNumbers.asp?page=" & intNumberOfPages) 'Reset QueryString due to user fault
      'OR Move to last page because QueryString page number is greater than the number of pages available
      'oRS.Move((intNumberOfPages - 1) * intRecordsPerPage) 'Moves the record cursor to the end of the last records shown, but does not change user's mistake in the QueryString
    End If
    
    'Display information about records for testing and trouble shooting errors
    Response.Write "<b>Records Per Page: </b>" & intRecordsPerPage & "<br />"
    Response.Write "<b>Record Count: </b>" & intRecordCount & "<br />"
    Response.Write "<b>Number Of Pages: </b>" & intNumberOfPages & "<br />"
    Response.Write "<b>Page Number: </b>" & intPageNumber & "<br />"
    Response.Write "<br />"
    'Display page number hyperlinks
    Response.Write strPageCount
    Response.Write "<br /><br />"
    
    'Display information in the database depending on selected page number
    Dim intCount
    Do Until intCount = intRecordsPerPage Or oRS.EOF
      intCount = intCount + 1
      Response.Write oRS("ID") & " " & oRS("fldInfo")
      Response.Write "<br />"
      oRS.MoveNext
    Loop
    %>
    I have including the script and an access database in a zip file, which should work after you, unzip and set the correct privileges for the database file/folder. I choose access for the database because those who know how to use MySQL will be able to convert the code easier than someone who does not.

    I have also set up an old brinkster account to host the script for anyone who wants to see it in action.
    http://www16.brinkster.com/terrafirma/pagenumbers.asp

    Please reply with any thoughts or changes I could do to make the code work better or more smoothly.
    Attached Files Attached Files


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •