Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 7th, 2005, 08:24 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default Next Page >> facility

I am looking for code that will dynamically show how many pages there are.

A good example of this is Google:
http://www.google.co.uk/search?hl=en&q=p2p&meta=
you will notice the Result Page at the foot of the page.

I have a variable count_rows that counts the number of rows in a MySQL table. This is relative to each page.

I want 50 records on each page, but how do i DYNAMICALLY create the links to show how many pages there are?

Picco

www.crmpicco.co.uk
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
 
Old March 7th, 2005, 10:11 AM
Friend of Wrox
 
Join Date: Jan 2004
Posts: 303
Thanks: 0
Thanked 0 Times in 0 Posts
Default

http://www.asp101.com/samples/db_paging.asp

 
Old March 7th, 2005, 10:57 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

How can i change this to suit MySQL DB instead of MSSQL?

________________________________-


<%
' BEGIN USER CONSTANTS
Dim CONN_STRING
Dim CONN_USER
Dim CONN_PASS

' I'm using a DSN-less connection.
' To use a DSN, the format is shown on the next line:
'CONN_STRING = "DSN=DSNName;"

CONN_STRING = "DBQ=" & Server.MapPath("database.mdb") & ";"
CONN_STRING = CONN_STRING & "Driver={Microsoft Access Driver (*.mdb)};"

' This DB is unsecured, o/w you'd need to specify something here
CONN_USER = ""
CONN_PASS = ""

' Our SQL code - overriding values we just set
' Comment out to use Access
CONN_STRING = "Provider=SQLOLEDB;Data Source=10.2.1.214;" _
    & "Initial Catalog=samples;Connect Timeout=15;" _
    & "Network Library=dbmssocn;"
CONN_USER = "samples"
CONN_PASS = "password"
' END USER CONSTANTS


' BEGIN RUNTIME CODE
' Declare our vars
Dim iPageSize 'How big our pages are
Dim iPageCount 'The number of pages we get back
Dim iPageCurrent 'The page we want to show
Dim strOrderBy 'A fake parameter used to illustrate passing them
Dim strSQL 'SQL command to execute
Dim objPagingConn 'The ADODB connection object
Dim objPagingRS 'The ADODB recordset object
Dim iRecordsShown 'Loop controller for displaying just iPageSize records
Dim I 'Standard looping var

' Get parameters
iPageSize = 10 ' You could easily allow users to change this

' Retrieve page to show or default to 1
If Request.QueryString("page") = "" Then
    iPageCurrent = 1
Else
    iPageCurrent = CInt(Request.QueryString("page"))
End If

' If you're doing this script with a search or something
' you'll need to pass the sql from page to page. I'm just
' paging through the entire table so I just hard coded it.
' What you show is irrelevant to the point of the sample.
'strSQL = "SELECT * FROM sample ORDER BY id;"

' Sept 30, 1999: Code Change
' Based on the non stop questions about how to pass parameters
' from page to page, I'm implementing it so I can stop answering
' the question of how to do it. I personally think this should
' be done based on the specific situation and is clearer if done
' in the same method on all pages, but it's really up to you.
' I'm going to be passing the ORDER BY parameter for illustration.

' This is where you read in parameters you'll need for your query.
' Read in order or default to id
'If Request.QueryString("order") = "" Then
' strOrderBy = "id"
'Else
' strOrderBy = Replace(Request.QueryString("order"), "'", "''")
'End If

' Make sure the input is one of our fields.
strOrderBy = LCase(Request.QueryString("order"))
Select Case strOrderBy
    Case "last_name", "first_name", "sales"
        ' A little pointless, but...
        strOrderBy = strOrderBy
    Case Else
        strOrderBy = "id"
End Select

' Build our SQL String using the parameters we just got.
strSQL = "SELECT * FROM sample ORDER BY " & strOrderBy & ";"

' Some lines I used while writing to debug... uh "test", yeah that's it!
' Left them FYI.
'strSQL = "SELECT * FROM sample WHERE id=1234 ORDER BY id;"
'strSQL = "SELECT * FROM sample;"
'Response.Write "SQL Query: " & strSQL & "<BR>" & vbCrLf


' Now we finally get to the DB work...
' Create and open our connection
Set objPagingConn = Server.CreateObject("ADODB.Connection")
objPagingConn.Open CONN_STRING, CONN_USER, CONN_PASS

' Create recordset and set the page size
Set objPagingRS = Server.CreateObject("ADODB.Recordset")
objPagingRS.PageSize = iPageSize

' You can change other settings as with any RS
'objPagingRS.CursorLocation = adUseClient
objPagingRS.CacheSize = iPageSize

' Open RS
objPagingRS.Open strSQL, objPagingConn, adOpenStatic, adLockReadOnly, adCmdText

' Get the count of the pages using the given page size
iPageCount = objPagingRS.PageCount

' If the request page falls outside the acceptable range,
' give them the closest match (1 or max)
If iPageCurrent > iPageCount Then iPageCurrent = iPageCount
If iPageCurrent < 1 Then iPageCurrent = 1

' Check page count to prevent bombing when zero results are returned!
If iPageCount = 0 Then
    Response.Write "No records found!"
Else
    ' Move to the selected page
    objPagingRS.AbsolutePage = iPageCurrent

    ' Start output with a page x of n line
    %>
    <p>
    Page <strong><%= iPageCurrent %></strong>
    of <strong><%= iPageCount %></strong>
    </p>
    <%
    ' Spacing
    Response.Write vbCrLf

    ' Continue with a title row in our table
    Response.Write "<table border=""1"">" & vbCrLf

    ' Show field names in the top row
    Response.Write vbTab & "<tr>" & vbCrLf
    For I = 0 To objPagingRS.Fields.Count - 1
        Response.Write vbTab & vbTab & "<th>"
        Response.Write objPagingRS.Fields(I).Name
        Response.Write "</th>" & vbCrLf
    Next 'I
    Response.Write vbTab & "</tr>" & vbCrLf

    ' Loop through our records and ouput 1 row per record
    iRecordsShown = 0
    Do While iRecordsShown < iPageSize And Not objPagingRS.EOF
        Response.Write vbTab & "<tr>" & vbCrLf
        For I = 0 To objPagingRS.Fields.Count - 1
            Response.Write vbTab & vbTab & "<td>"
            Response.Write objPagingRS.Fields(I)
            Response.Write "</td>" & vbCrLf
        Next 'I
        Response.Write vbTab & "</tr>" & vbCrLf

        ' Increment the number of records we've shown
        iRecordsShown = iRecordsShown + 1
        ' Can't forget to move to the next record!
        objPagingRS.MoveNext
    Loop

    ' All done - close table
    Response.Write "</table>" & vbCrLf
End If

' Close DB objects and free variables
objPagingRS.Close
Set objPagingRS = Nothing
objPagingConn.Close
Set objPagingConn = Nothing


' Show "previous" and "next" page links which pass the page to view
' and any parameters needed to rebuild the query. You could just as
' easily use a form but you'll need to change the lines that read
' the info back in at the top of the script.
If iPageCurrent > 1 Then
    %>
    <a href="db_paging.asp?page=<%= iPageCurrent - 1 %>&order=<%= Server.URLEncode(strOrderBy) %>">[&lt;&lt; Prev]</a>
    <%
End If

' You can also show page numbers:
For I = 1 To iPageCount
    If I = iPageCurrent Then
        %>
        <%= I %>
        <%
    Else
        %>
        <a href="db_paging.asp?page=<%= I %>&order=<%= Server.URLEncode(strOrderBy) %>"><%= I %></a>
        <%
    End If
Next 'I

If iPageCurrent < iPageCount Then
    %>
    <a href="db_paging.asp?page=<%= iPageCurrent + 1 %>&order=<%= Server.URLEncode(strOrderBy) %>">[Next &gt;&gt;]</a>
    <%
End If

' END RUNTIME CODE
%>


www.crmpicco.co.uk
 
Old March 7th, 2005, 12:20 PM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

I have this code from planet-source-code.com, but i need something along these lines to work with my existing code. How can i take just the Page 1-2-3-4-5 etc, etc... section and put this into my existing code. I dont want to re-write my entire script just for this small section at the bottom. Basically, is there anyway i can have the DB paging in around 20 lines - instead of the 100-odd below? Currently my asp page returns 50 pages and when i click 'Next 50 >>' it gives me records 50-100 and so on. I can post my whole script if required.


<%
Dim sDatabaseConnection
sDatabaseConnection = "DRIVER={MySQL ODBC 3.51 Driver};"_
& "SERVER=localhost;"_
& "DATABASE=test;"_
& "UID=root;PWD=; OPTION=35;"
'################################################# #####################################
'# COUNT RECORDS FIRST
'################################################# #####################################
Dim sQueryStatus

Dim intRecordsPerPage
Dim intPages
Dim sLimitPart 'LIMIT 0,1 -----> LIMIT {START_NUMBER,RECORDS_PERPAGE}
Dim sStartQuery
Dim sMaxStart
Dim sProjectPrev
Dim sProjectNext
intRecordsPerPage = 50 'How many records to show per page

Dim sqlProjectCount
Dim connCount, rsCount
Dim sProjectCount
sqlProjectCount = "select count(*) from trip_master where agencyid = '"&agencyid&"' and regular_trip <> 'Cancelled'"
'Response.Write(sqlProjectCount)
Set connCount = Server.Createobject("ADODB.Connection")
Set rsCount = Server.CreateObject("ADODB.Recordset")
connCount.open sDatabaseConnection
Set rsCount = connCount.Execute(sqlProjectCount)
If rsCount.eof then
sProjectCount = 0
Else
sProjectCount = rsCount.Fields(0) 'number of records
End If
connCount.close
set connCount=nothing
set rsCount=nothing


'################################################# #####################################
'# SIMPLE PAGING
'################################################# #####################################
sStartQuery = Request.QueryString("start")

'Get total pages
If intRecordsPerPage < sProjectCount then
intPages = sProjectCount / intRecordsPerPage
End If

'here we modify the number if it has a decimal, a better solution maybe would be to use formatnumber
Dim instrIntPages
instrIntPages = Instr(intPages,".")

'If after the decimal there is a 0 then we need to add a page
If instrIntPages > 0 then
intPages = Left(intPages,instrIntPages) + 1
End If

'Lets create the limit for the sql
'LIMIT 0,1 -----> LIMIT {START_NUMBER,RECORDS_PERPAGE}
If sStartQuery <> "" AND isNumeric(sStartQuery) then
sLimitPart = "LIMIT " & sStartQuery & "," & intRecordsPerPage
Else
sLimitPart = "LIMIT " & "0," & intRecordsPerPage
End If

'Lets figure out what the max start number is
sMaxStart = (intPages*intRecordsPerPage)-intRecordsPerPage

If sStartQuery <> "" AND isNumeric(sStartQuery) then
sStartQuery = CINT(sStartQuery)
Else
sStartQuery = 0
End If


'Now lets create our previous / next buttons and disable them if they are not needed.
If sMaxStart < 0 then sMaxStart = 0

If sStartQuery = "" or sStartQuery = "0" then 'PREVIOUS DISABLED
sProjectPrev = "<a span class='sgrey'>Previous</span>"
ElseIf sStartQuery > 0 then 'PREVIOUS ENABLED
sProjectPrev = "<a class='elinks' href='?start=" & sStartQuery - intRecordsPerPage & "'>Previous</span>"
End If

'Next
If sMaxStart = sStartQuery then 'NEXT DISABLED
sProjectNext = "<a span class='sgrey'>Next</span>"
ElseIf sStartQuery < sMaxStart then 'NEXT ENABLED
sProjectNext= "<a class='elinks' href='?start=" & sStartQuery + intRecordsPerPage & "'>Next</span>"
End If


'Now its time to select our projects
Dim connP, rsP
Dim sqlProjects

Dim sPid
Dim sSubject

sqlProjects = "select * from trip_master where agencyid = '"&agencyid&"' and newdate > '" &new_date& "' and regular_trip <> 'Cancelled' limit " &vara& ", 50" & sLimitPart

'Response.Write(sqlProjects)
Set connP = Server.Createobject("ADODB.Connection")
Set rsP = Server.CreateObject("ADODB.Recordset")
connP.open sDatabaseConnection
Set rsP = connP.Execute(sqlProjects)
If NOT rsP.eof then
Do while not rsP.eof
sPid = rsP("A1_pnr")
sSubject = rsP("leadname")
rsP.movenext
loop
Dim queryPageOn
queryPageOn = Request.QueryString("page")
If NOT isNumeric(queryPageOn) AND NOT sStartQuery = "0" then
queryPageOn = "1"
End If
queryPageOn = CINT(queryPageOn)
Response.Write(sProjectPrev & "&nbsp;&nbsp;")
'# Create Page Numbers & Links
Dim iPages, iPagesTemp
For iPages=1 to intPages
iPagesTemp = iPages-1
If NOT queryPageOn=iPages then
Response.write "<a href='?start=" & iPagesTemp*intRecordsPerPage & "&page="& iPages & "' class='elinks'>" & iPages & "</a>&nbsp; "
Else
Response.write "<span class='greyt'>[</span><a href='?start=" & iPagesTemp*intRecordsPerPage & "&page="& iPages & "' class='elinks'>" & iPages & "</a><span class='greyt'>]</span>&nbsp; "
End If
Next
Response.Write("&nbsp;&nbsp;"&sProjectNext)

%>
<%Else%>
<tr>
<td bgcolor="#FFFFFF" class="sred"> No projects under this category at this time. </td>
<td width="22%" bgcolor="#FFFFFF"><div align="center"></div></td>
<td bgcolor="#FFFFFF">&nbsp;</td>
</tr>
<%End IF
connP.close
set connP=nothing
set rsP=nothing
'################################################# ########################################
%>

www.crmpicco.co.uk
 
Old March 7th, 2005, 12:34 PM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

Is this any good?

______________________________

 Const adOpenForwardOnly = 0
 Const adLockReadOnly = 1
 Const adUseClient = 3
 Const adCmdText = 1


 IF Request.QueryString("P") <> "" THEN
         nPage = CInt(Request.QueryString("P"))
 ELSE
         nPage = 1
 END IF


 Response.Write "<P>nPage: " & nPage & "</P>"


 nPageSize = 5


 rs.PageSize = nPageSize
 rs.CursorLocation = adUseClient


 rs.Open "SELECT * FROM trip_master ORDER BY a1_pnr DESC", Conn, adOpenForwardOnly, adLockReadOnly, adCmdText
 IF NOT rs.EOF AND NOT rs.BOF THEN
         nPages = rs.PageCount
         nRecs = rs.RecordCount


         IF nPage > nPages THEN nPage = nPages
         IF nPage < 1 THEN nPage = 1
         rs.AbsolutePage = nPage


         FOR i=1 TO nPageSize
         IF NOT rs.EOF THEN
                 Response.Write "<P>" & rs("N_Text") & "</P>"
                 rs.MoveNext
         END IF
         NEXT
 END IF
 rs.Close


 IF nPage > 1 THEN
         Response.Write "<A HREF=""paging.asp?P=" & nPage - 1 & """ TITLE=""Zur vorigen Seite""><<</A> "
 ELSE
         Response.Write "<< "
 END IF


 FOR i = 1 TO nPages
         Response.Write "<A HREF=""paging.asp?P=" & i & """ TITLE=""Zur " & i & ". Seite"">" & i & "</A> "
 NEXT


 IF nPage < nPages THEN
         Response.Write "<A HREF=""paging.asp?P=" & nPage +1 & """ TITLE=""Zur nächsten Seite"">>></A>"
 ELSE
         Response.Write ">>"
 END IF


www.crmpicco.co.uk
 
Old March 7th, 2005, 12:58 PM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

I have this code which will give me HOW many pages there should be. How can i DYNAMICALLY create hyperlinks that will show Page 1 - 2 - 3 etc, etc...

count_rows_div = count_rows/50
response.write "count rows (divided) = " & round(count_rows_div+0.99) & "<br>"
pages = round(count_rows_div+0.99)

counter = 0
for i = 1 to pages
counter = counter + 1
response.write "count<br>"
'response.write "<a href='bookdates_picco.asp?vara=51'>Page 1</a>"
next
response.write "cntr = " & counter & "<br>"

www.crmpicco.co.uk
 
Old March 7th, 2005, 01:18 PM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

This is the code now, more what i am after: But in the querystring i need to pass :

bookdates_picco.asp?timeoption=<%=timeoption%>&sea rchby=<%=user%>&agencyid=<%=agencyid%>&vara=<%=var a+51%>.

Can anyone shed some light on this as i am kinda stuck and not sure where to look at times, joe?


    Dim intRecordsPerPage
    intRecordsPerPage = 50

  Dim queryPageOn
      queryPageOn = count_rows

    If NOT isNumeric(queryPageOn) then
        queryPageOn = "1"
    End If
        queryPageOn = CINT(queryPageOn)
    Response.Write(sProjectPrev & "&nbsp;&nbsp;")
    '# Create Page Numbers & Links

    Dim iPages, iPagesTemp
    For iPages=1 to pages
    iPagesTemp = iPages-1
    If NOT queryPageOn=iPages then
    Response.write "<a href='?vara=" & iPagesTemp*intRecordsPerPage & "&page="& iPages & "' class='elinks'>" & iPages & "</a>&nbsp; "
    Else
    Response.write "<span class='greyt'>[</span><a href='?vara=" & iPagesTemp*intRecordsPerPage & "&page="& iPages & "' class='elinks'>" & iPages & "</a><span class='greyt'>]</span>&nbsp; "
    End If
    Next
    Response.Write("&nbsp;&nbsp;"&sProjectNext)


www.crmpicco.co.uk
 
Old March 10th, 2005, 08:59 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

This code below works for the 'previous' button to allow me to go to the previous 50 records.
    Dim intRecordsPerPage
    intRecordsPerPage = 50

  Dim queryPageOn
      queryPageOn = count_rows

    If NOT isNumeric(queryPageOn) then
        queryPageOn = "1"
    End If
        queryPageOn = CINT(queryPageOn)
    Response.Write(sProjectPrev & "&nbsp;&nbsp;")

    response.write "<div align=center>"
    '# Create Page Numbers & Links

    sStartQuery = vara
    'response.write "sStartQuery = " & sStartQuery & "<br>"

    If sStartQuery = "" or sStartQuery = "0" then 'PREVIOUS DISABLED
    sProjectPrev = "<a span class='sgrey'>Previous</span>"
    response.write "<a span class='sgrey'>Previous</span>"
    ElseIf sStartQuery > 0 then 'PREVIOUS ENABLED
    sProjectPrev = "<a class='elinks' href='?start=" & sStartQuery - intRecordsPerPage & "'>Previous</span>"
    'response.write "<a class='elinks' href='?start=" & sStartQuery - intRecordsPerPage & "'>Previous</span>"
    response.write "<a class='elinks' href=# onclick='javascript: history.back()'>Previous</span>"

    End If

But how do i do it for the next 50?

Picco

www.crmpicco.co.uk
www.milklemonadechocolate.uk.tt





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chpt 7 >> Pg 245 >>Try It Out #4-5 harrison4411 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 March 2nd, 2006 06:26 PM
Achitecture ? SQL -> XML -> ASP -> PDF or HTML Frm jstrong Classic ASP XML 0 July 9th, 2005 01:18 PM
<style> tags in a <body> vs. <div> bcat BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 1 March 27th, 2005 08:50 AM
Chapter 4> ERROR using "=>>>" guiro BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 5 January 13th, 2005 06:38 PM
VB.Net -> Filename -> DTS Package -> tempdB daniel Pro VB.NET 2002/2003 1 October 7th, 2004 01:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.