www.webdeveloper.com
Results 1 to 10 of 10

Thread: dynamic page based on contents of database

  1. #1
    Join Date
    Feb 2003
    Location
    Sault Ste Marie, On, Canada
    Posts
    168

    dynamic page based on contents of database

    Hey I have a comments page that displays all the comments in my database, problem is all of them appear on the page so it's super long. How do I only show 10 comments per page, and at the bottom is view page 1,2,3,4,....
    that i want to appear automatically without having to code that in as the database grows, i need it to be dynamic as the database grows. Some sample code would be great too. Thx.
    Come on ribeye I know you have the answer!!

  2. #2
    Join Date
    Jan 2003
    Location
    Dundee, Scotland
    Posts
    1,367
    hi,
    here is the code i retrieve from www.aspfaq.com . I used this example then worked from there.

    Code:
    <% 
        set conn = Server.CreateObject("ADODB.Connection") 
        conn.open "<connection_string>" 
     
        ' tell me how many records you want per page  
        perPage = 50  
     
        ' set up a query to get the count, and another to get the data  
        countSQL = "SELECT count(artist) FROM CDs"  
        dataSQL = "SELECT artist,title,cdn FROM CDs"  
     
        ' start getting the data, first we need to know the count  
        ' this will tell us how many pages there will be  
        set rs = conn.execute(countSQL)  
        counter = clng(rs(0))  
        rs.close: set rs = nothing  
     
        ' determine if anything is returned. If there are records,  
        ' figure out the pagecount and the total number of records.  
        if counter < 1 then  
            Response.Write "No records found."  
            Response.End  
        else  
     
            if counter = 1 then perpage = 1  
            totalRecords = counter  
            pageCount = clng(counter/perpage)  
            if counter/perPage > pageCount then  
                pageCount = pageCount + 1  
            end if  
         
            ' what page are we on? 
            currentpage = clng(request.form("currentpage"))  
            if currentpage <= 1 then  
                currentpage = 1  
            elseif currentpage > pageCount then  
                currentpage = pagecount  
            end if 
            cp = currentPage 
            response.Write "<form method=post action=rsPage.asp " 
            response.Write "name=spform><b>Aaron's CDs</b> ... " 
            response.Write "count = " & counter & "</b> ... Jump to" 
            response.Write " page #<select name=currentpage " 
            response.Write "onchange='document.spform.submit();'>" 
            for i = 1 to pageCount 
                thisOpt = "<option value='" & i & "'>" & i 
                ' highlight the current page! 
                response.Write replace(thisOpt," value='" & cp &_ 
                    "'", " value='" & cp & "' SELECTED") 
            next 
            Response.Write "</select></form><p>" 
            Response.Write "<p><table border=0 cellpadding=4 " 
            Response.Write "cellspacing=0 style='border:1px solid #999999'>" 
        end if  
     
        ' set up the first record to move to, if applicable  
        if pageCount > 1 then  
            startrecord = (clng(currentpage-1) * perpage) + 1  
        end if  
     
     
        ' let's get the data, move to the first record if necessary  
        ' loop through, returning records until we meet perpage or EOF  
     
        set rs = conn.execute(dataSQL) 
        'if counter > perpage and startrecord > 1 then 
        rs.move(startrecord-1)  
        for x = 1 to perpage  
            if rs.eof then exit for  
            response.write "<tr valign=middle><td>" 
            artist = rs(0) 
            if artist <> prevArtist then 
                prevArtist = artist 
                if rs(2) then 
                  artist = "<img src=leaf.gif><b>" & artist & "</b>" 
                else 
                  artist = "<b>" & artist & "</b>" 
                end if 
            else 
              artist = "  <span style='color:#999999'>" & artist & "</span>" 
            end if 
            response.write "<nobr>" & artist & "</nobr></td><td>" 
            response.write "<nobr>" & rs(1) & "</nobr></td></tr>" 
            rs.movenext  
        next 
        response.write "</table>" 
     
        ' clean up  
        rs.close: set rs = nothing  
        conn.close: set conn = nothing  
    %>
    ----------------------------------

  3. #3
    Join Date
    Feb 2003
    Location
    Sault Ste Marie, On, Canada
    Posts
    168

    Angry Annoying problem!!!!!

    Here is the code I've tried switching the code around but when I've tried switching the code around so it's put where it isn't open and I get error object required. I've tried closing it and I get same error below but when object is closed. I'm loosing more hair here. This code is from Dave, so he may know, and I know ribby always has the answer! thanks guys

    Error Type:
    ADODB.Recordset (0x800A0E79)
    Operation is not allowed when the object is open.
    /hunting/wwwroot/View_Comments.asp, line 15


    Dim dbConn, dbQuery, recordsetDB, strComments, strQueryString1,strUser_Id
    Dim Hunt_Id, Hunt_Password, numPages, curPage

    Set dbConn = Server.CreateObject("ADODB.Connection")
    Call dbConn.Open ("Appdb1","Anyone","Anyone")

    strquerystring1 = "SELECT Comments.Comments, Comments.Hunt_Id FROM Comments "

    set recordsetDB = dbconn.execute(strquerystring1)
    IT DOESN"T LIKE THIS LINE UNDERNEATH
    RecordsetDB.CursorLocation = 3
    RecordsetDB.PageSize = 10



    numPages = RecordsetDB.PageCount

    curPage = CInt(Request.QueryString("Page"))
    If curPage < 1 Then curPage = 1
    If curPage > numPages Then curPage = numPages
    RecordsetDB.AbsolutePage = curPage


    %>
    <html>

    <head>
    <title>View Comments</title>
    </head>

    <body background="grback.jpg">
    Page <%=curPage%> of <%=numPages%>

    <%Do While recordsetDB.AbsolutePage = curPage And NOT recordsetDB.EOF%>

  4. #4
    Join Date
    Feb 2003
    Location
    Sault Ste Marie, On, Canada
    Posts
    168
    Hey dave I have a question what does this line mean, you said used for paging, CursorLocation and why =3?

    recordsetDB.CursorLocation = 3

    And this one CInt? never seen that before Character Integer?
    And is ("Page") a built in funcition?
    curPage = CInt(Request.QueryString("Page"))

    And could you explain Absolute page. Does this mean as the page is? the current page displayed?

  5. #5
    Join Date
    Feb 2003
    Location
    Sault Ste Marie, On, Canada
    Posts
    168
    Thx Dave much appreciated, by the way I got the code working great, my site is almost done, when it is i'll send you a link so you can see what you've been helping me with all these weeks.

    Alex

  6. #6
    Join Date
    Feb 2003
    Location
    Sault Ste Marie, On, Canada
    Posts
    168

    Won't let me pass variables to other page

    Ok the user id and pass are passed to the first page and I can physically see them, as soon as I press page next, last page, etc.. they dissapear totally, nothing is getting passed after I press those buttons, I've moved the <input type="text" name="OldHunt_Id" size="24" Value="<%=Hunt_Id%>"> and requestl.form line to every possible location i can think of, no results.


    strquerystring1 = "SELECT Stories.Stories, Stories.Hunt_Id FROM Stories "
    Set recordsetDB = server.createObject("ADODB.recordset")
    recordsetDB.CursorLocation =3
    recordsetDB.PageSize =10
    recordsetDB.Open strquerystring1, dbconn
    numpages = recordsetDb.PageCount
    CurPage = CInt(request.querystring("Page"))
    If curpage < 1 Then Curpage = 1
    If curpage > Numpages Then Curpage = Numpages
    recordsetDB.AbsolutePage = Curpage
    %>

    <html>

    <head>
    <title>View Stories</title>
    </head>
    <body background="grback.jpg">
    Page <%=CurPage%> Of <%=NumPages%>
    <% Do While recordsetDb.AbsolutePage = Curpage AND NOT recordsetDB.EOF%>
    <table border="3" width="504" height="100" bordercolordark ="darkGreen" bordercolorlight="lightgreen">
    <tr>
    <td>
    <%
    strStories = recordsetDB("Stories").VALUE
    strUser_Id = recordsetDB("Hunt_Id").Value %>
    Submitted by <% response.write ""& strUser_Id & "" %><P>
    <%response.write ""& strStories &"" %>

    </td>
    </tr>
    </table>
    <%
    recordsetDB.MoveNext

    Loop
    %>

    <form Name ="Navigate" METHOD="POST" ACTION="View_Storie.asp" onSubmit="return false;">

    <input type="text" name="Hunt_Password" size="24" Value="<%=Hunt_Password%>">
    <input type="text" name="OldHunt_Id" size="24" Value="<%=Hunt_Id%>">
    <input type="Submit" value="First Page" onClick="
    self.location.href = '<%=Session("Page")%> ?Page=1';
    return true;">
    <input type="Submit" value="Prev Page" onClick="
    self.location.href = '<%=Session("Page")%>?Page=<%=(curPage-1)%>';
    return true;">
    <input type="Submit" value="Next Page" onClick="self.location.href =
    '<%=Session("Page")%>?Page=<%=(curPage+1)%>';
    return true;">
    <input type="submit" value="Last Page" onClick="
    self.location.href = '<%=Session("Page")%>?Page=<%=numPages%>';return true;">
    </form>


    <form NAME="Back_TO_Main" METHOD="POST" ACTION="LoggedMainPics.asp">
    <input type="hidden" name="Hunt_Password" size="24" Value="<%=Hunt_Password%>">

    <input type="hidden" name="OldHunt_Id" size="24" Value="<%=Hunt_Id%>">GO BACK
    TO THE MAIN MENU <input type="submit" value="GO" name="backview">
    </form>

  7. #7
    Join Date
    Feb 2003
    Location
    Sault Ste Marie, On, Canada
    Posts
    168
    Noted

  8. #8
    Join Date
    Jan 2003
    Location
    Dundee, Scotland
    Posts
    1,367
    Hi,
    i ran this code and it works fine with me. I set up a test database created your table added in data ran the page, all the buttons are there, the record count is correct, all the button are working and done what they are ment to do. I never got any errors and none of the buttons disapeared. Is it not working for you?
    ----------------------------------

  9. #9
    Join Date
    Feb 2003
    Location
    Sault Ste Marie, On, Canada
    Posts
    168
    Yes you have id'd it. But i'm still not sure how to get the need user id and pass to each page, because when you click to go back to main the login script is run and since no id and pass you get booted! Could this be done with a javascript for when you press the button the query is run and sends the needed data to page 2,3,etc.. and calling the javascript to send the id and pass? O ya ribby the buttons do work sorry for the extra work i created for you, i maybe should of explained it better.
    My apologies

    Alex
    Last edited by Calmaris; 03-20-2003 at 05:25 PM.

  10. #10
    Join Date
    Feb 2003
    Location
    Sault Ste Marie, On, Canada
    Posts
    168
    For some reason it's still not passing the 2 variables. I'm showing it plain as day on the first page and it's blank on the rest after you press the buttons.

    <form Name ="Navigate" onSubmit="return false;">

    <input type="button" value="First Page" onClick="
    self.location.href = '<%=Session("Page")%>?OldHunt_Id =<%=Hunt_Id%>&Hunt_Password =<%=Hunt_Password%>&Page=1';
    return true;">
    <input type="button" value="Prev Page" onClick="
    self.location.href = '<%=Session("Page")%>?OldHunt_Id =<%=Hunt_Id%>&Hunt_Password =<%=Hunt_Password%>&Page=<%=(curPage-1)%>';
    return true;">
    <input type="button" value="Next Page" onClick="self.location.href =
    '<%=Session("Page")%>?OldHunt_Id = <%=Hunt_Id%>&Hunt_Password =<%=Hunt_Password%>&Page=<%=(curPage+1)%>';
    return true;">
    <input type="button" value="Last Page" onClick="
    self.location.href = '<%=Session("Page")%>?OldHunt_Id =<%=Hunt_Id%>&Hunt_Password =<%=Hunt_Password%>&Page=<%=numPages%>';return true;">
    </form>


    <form NAME="Back_TO_Main" METHOD="POST" ACTION="LoggedMainPics.asp">
    <input type="text" name="Hunt_Password" size="24" Value="<%=Hunt_Password%>">

    <input type="text" name="OldHunt_Id" size="24" Value="<%=Hunt_Id%>">GO BACK
    TO THE MAIN MENU <input type="submit" value="GO" name="backview">
    </form>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles