SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot frosco's Avatar
    Join Date
    Jul 2003
    Location
    WA state
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Loop/Do Only Showing One Record

    I'm new to the Loop/Do stuff. This code is displaying all duplicate records. My connection string is the same as other pages that return all records. The Query is pulling * from the database. What am I missing? Thanks!
    Code:
    <%
    set MyConnection = server.createobject("ADODB.Connection")
    MyConnection.Open "DSN=RHSAA;"
    
    set rsGuestbook = server.createobject("ADODB.recordset")
    
    'Select Query
    	SQLQuery = "SELECT * FROM Guestbook ORDER BY Last, First "
    	set rsGuestbook = MyConnection.Execute(SQLQuery)
    
    
    dim last, first, id, newname, formid
    newname=rsGuestbook("NewName")
    last=rsGuestbook("Last")
    first=rsGuestbook("First")
    id=rsGuestbook("id")
    email=rsGuestbook("Email")
    formid=request.form("formid")
    
    %>
    </head>
    
    <body>
    
    <form action="password.asp" method="GET">
      <p>1. Select Your Name from the List</p>
    <%
    ' Begin Select
    	response.write "<select name='formid' size='1'>"
    	response.write "<option selected value='Select Your Name'>Select Your Name</option>"
    	response.write "<option> </option>"
    
    ' Begin Loop
    	Do Until rsGuestbook.EOF
    
    ' Select Content
    	Response.Write "<option value='"
    	response.write id
    	response.write "'>"
    	response.write last
    	response.write ",&nbsp;"
    	response.write first
    	response.write "</option>"
    
    'Instruct Loop
    	rsGuestbook.MoveNext
    	Loop
    
    ' Finish Select
    	response.write "</select>"
    	response.write "<input type='submit' value='Next' >"
    	response.write "</form>"
    %>
    </form>
    </body>
    </html>

  2. #2
    SitePoint Zealot TheTank's Avatar
    Join Date
    May 2003
    Location
    Houston, TX
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    move this:
    Code:
    newname=rsGuestbook("NewName")
    last=rsGuestbook("Last")
    first=rsGuestbook("First")
    id=rsGuestbook("id")
    email=rsGuestbook("Email")
    to just below:
    Code:
    ' Begin Loop
    	Do Until rsGuestbook.EOF
    you're setting your variables outside teh loop so the script never knows to reset to variable names as it loops
    I think sometimes I dream in code.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2005
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1. try not to get into the habit of enclosing vars w/single quotes, use double
    2. use one request.write statement instead of many, cuts down on server
    to client processing...simply concatenate the string w/ & _ and the
    vbCrlf will move the next line down so your source code is readible
    3. no need to have value=""selected"" in this <option>Select Your Name</option>
    4. you had 2 </form> tags
    5. you can mix back and forth between asp and html, so if you wanted to you
    can simply just used asp to loop the values:
    6. since you have the recrdset open, simply use the recordset field value in
    option statement
    7. close out recordset when done

    <form>
    <select>
    <option>Select Name</option>

    <%
    do While not rsGuestbook.EOF
    %>

    <option value="<%=rsGuestbook("id")%>"><%=rsGuestbook("Last") & ", " & rsGuestbook("First")%></option>

    <%
    rsGuestbook.MoveNext
    Loop
    %>

    </select>

    hoped this helped..


    Code:
    <html>
     <head>
    </head>
    <body>
    
    <%
        dim SQLQuery,  email, formid
    
       set MyConnection = server.createobject("ADODB.Connection")
       MyConnection.Open "DSN=RHSAA;"
    
       set rsGuestbook = server.createobject("ADODB.recordset")
    
       
      ' Select Query
    	SQLQuery = "SELECT id, Last, First, NewName, Email " & _
                                  "FROM Guestbook " & _
                                  "ORDER BY Last, First;"
    
        set rsGuestbook = MyConnection.Execute(SQLQuery)    
    
        email=rsGuestbook("Email")
        formid=request.form("formid")
    %>
    
    <form action="password.asp" method="GET">
      <p>1. Select Your Name from the List</p>
    
    <%
    ' Begin Select
    	response.write "<select name=""formid"">" & vbCrlf & _
                                     "<option value="""">Select Your Name</option>"
    	
    ' Begin Loop
    	Do Until rsGuestbook.EOF
    
    ' Select Content
                 Response.Write "<option value=""" & rsGuestbook("id") & """>" & rsGuestbook("Fast") & ",&nbsp;" & rsGuestbook("Frst") & "</option>"
    
    'Instruct Loop
    	rsGuestbook.MoveNext
    	Loop
                rsGuestbook.Close
    
    ' Finish Select
    	response.write "</select>" & vbCrlf & _
                                     "<input type=""submit"" value=""Next"">" & vbCrlf & _
                                     "</form>"
    
    %>
    
    </body>
    </html>

  4. #4
    SitePoint Zealot frosco's Avatar
    Join Date
    Jul 2003
    Location
    WA state
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you! That did the trick.

    I'm still learning--I did close out the recordset, just didn't show it in my code. I'm using MS FrontPage and it likes to add extra things when I save, especially in this <select>, I found that using multiple <% %> kept it from doing that.

    Hopefully I can afford a better program at some point! I really appreciate your help & tips.


Bookmarks

Posting Permissions

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