problem with nested loop (I think)

Discussion in 'ASP General' started by middletree, Jan 23, 2009.

  1. middletree

    middletree Guest

    A few days ago, I posted a problem, and the answer given has led me to this
    point. I think I have the idea right, but the syntax isn't quite there.

    Using classic ASP, I have a page which allows users to enter some info into
    a table called Personal.

    Also on the same form is some checkboxes, which are populated by a mostly
    static table we'll call Gift.

    Because it's a many-to-many relationship (that is, a user can check more
    than one checkbox), I have created a table we'll refer to as table
    PersonalGift.

    It's a union table for resolving the many-to-many between tables Personal
    and Gift. It consists of only two fields: the ID (Primary Key) of each,
    called PersonaID and GiftID.

    This works fine, but now I'm asked to create an edit page. A user needs to
    be able to click a name, which will take them to a page which displays the
    information which was entered in the original form. For most of the info,
    which was stored in the Personal table, this is no problem. But I need to be
    able to list all 25 checkboxes (dynamically built from the Gift table), and
    if there is a match for that person (using the Personal table's PK), I need
    to have that checkbox to be checked, and if not, it needs to be unchecked.

    Just to be clear, the objective here is to populate, on page load, certain
    checkboxes, depending on what'salreay in the database.

    It seems like I could do this by doing a SELECT from table PersonalGift:
    SELECT GiftID
    FROM PersonalGIft
    WHERE PersonalID = 2 (2 being an example, pulled in from the querystring)

    If it turns out that there are rows in table PersonalGift like this:
    PersonalID GiftID

    2 2
    2 4
    2 8


    this would give me a result set of (for example) 2,4,8

    So I just have to build my checkboxes to display the word "checked" if the
    ID of that checkbox is somewhere in that resultset.

    As everyone here probably knows, in HTML, a checkbox which is prefilled with
    a check in the checkbox on page load looks like this:

    <input type=checkbox value=whatever checked>

    in contrast, a checkbox with the below code will not be checked on page
    load:

    <input type=checkbox value=whatever>


    Anyway, that's my goal here. Display the checkboxes, and display certain
    ones as checked with the page loads.

    So here's how I am doing it. Assuming I use the SQL code mentioned earlier,
    I will have a resultset that contains all the IDs for all 25 boxes. We'll
    call that rsAllBoxes. The resultset containing the checked ones (2,4,8)
    should ideally be put into an array:

    arrChecked = rsChecked.GetRows()

    Now we iterate the array as many times as we like, which we want to do each
    time we write a checkbox:

    Do Until rsAllBoxes.EOF
    Response.Write "<input type=""checkbox"" id=""" & rsAllBoxes("ID") & """"
    For i = 0 To Ubound(arrChecked,2)
    If Cint(arrChecked(0,i) = Cint(rsAllBoxes("ID") Then Response.Write "
    checked=""checked"""
    Next
    Response.Write ">" & rsAllBoxes("TextValue") & "<br />"
    rsAllBoxes.MoveNext
    Loop




    Needless to say, it's displaying all sorts of VB Script errors.

    Any ideas?
     
    middletree, Jan 23, 2009
    #1
    1. Advertising

  2. middletree

    Evertjan. Guest

    middletree wrote on 23 jan 2009 in
    microsoft.public.inetserver.asp.general:

    > Do Until rsAllBoxes.EOF
    > Response.Write "<input type=""checkbox"" id=""" & rsAllBoxes("ID") &
    > """" For i = 0 To Ubound(arrChecked,2)
    > If Cint(arrChecked(0,i) = Cint(rsAllBoxes("ID") Then Response.Write "


    You are missing some closing ))) here

    Cint(arrChecked(0,i)) = Cint(rsAllBoxes("ID"))


    HTML ERROR: a numeric is iss not allowed,
    all id-s should start with a letter.

    Seems to me you probably ment:
    Cint(arrChecked(2,i))
    ???

    > checked=""checked"""


    Writing a space & the word 'checked' suffices,
    Are you sure you never write it more that once?

    > Next
    > Response.Write ">" & rsAllBoxes("TextValue") & "<br />"


    /> ???
    Are you writing xml?

    > rsAllBoxes.MoveNext
    > Loop
    >


    You should look at the rendered source as provided by your
    browser and most could be revealed, as I sullose there are some missing
    spaces.

    For better visibility and not having to count the """"" use:

    <form ...>
    <%
    Do Until rsAllBoxes.EOF
    %>
    <input type='checkbox' id='<%=rsAllBoxes("ID")%>'
    <%
    ''' warning, probable numeric id html error '''

    temp = ""
    For i = 0 To Ubound(arrChecked,2)
    If Cint(arrChecked(0,i)) = Cint(rsAllBoxes("ID")) Then
    temp = " checked"
    End If
    Next
    %>
    <% = temp%>> <% = rsAllBoxes("TextValue") %><br>
    <%
    rsAllBoxes.MoveNext
    Loop

    %>
    </form>



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jan 23, 2009
    #2
    1. Advertising

  3. Gazing into my crystal ball I observed "Evertjan."
    <> writing in news:Xns9B9C95E5D3723eejj99@
    194.109.133.242:

    > Writing a space & the word 'checked' suffices,
    > Are you sure you never write it more that once?
    >


    Depending on the flavor of the HTML, XHTML cannot have empty attributes, so
    it must be checked="checked" for XHTML.

    --
    Adrienne Boswell at Home
    Arbpen Web Site Design Services
    http://www.cavalcade-of-coding.info
    Please respond to the group so others can share
     
    Adrienne Boswell, Jan 23, 2009
    #3
  4. middletree

    Evertjan. Guest

    Adrienne Boswell wrote on 23 jan 2009 in
    microsoft.public.inetserver.asp.general:

    > Gazing into my crystal ball I observed "Evertjan."
    > <> writing in
    > news:Xns9B9C95E5D3723eejj99@ 194.109.133.242:
    >
    >> Writing a space & the word 'checked' suffices,
    >> Are you sure you never write it more that once?
    >>

    >
    > Depending on the flavor of the HTML, XHTML cannot have empty
    > attributes, so it must be checked="checked" for XHTML.
    >


    Sorry, my influenza precludes me from tasting flavors today.


    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Jan 23, 2009
    #4
  5. Gazing into my crystal ball I observed "Evertjan."
    <> writing in news:Xns9B9CA9AAE8699eejj99@
    194.109.133.242:

    > Adrienne Boswell wrote on 23 jan 2009 in
    > microsoft.public.inetserver.asp.general:
    >
    >> Gazing into my crystal ball I observed "Evertjan."
    >> <> writing in
    >> news:Xns9B9C95E5D3723eejj99@ 194.109.133.242:
    >>
    >>> Writing a space & the word 'checked' suffices,
    >>> Are you sure you never write it more that once?
    >>>

    >>
    >> Depending on the flavor of the HTML, XHTML cannot have empty
    >> attributes, so it must be checked="checked" for XHTML.
    >>

    >
    > Sorry, my influenza precludes me from tasting flavors today.
    >
    >


    Hope you feel better soon. Try this, it works every time for me.
    Take a clove of fresh garlic and cut it up into pieces about the same
    size as a vitamin, then take those garlic pieces with a glass of water.
    Have a cup of tea with some honey, then bundle yourself up and sleep for
    a few hours. Fever should break pretty easily, and you'll feel better.

    --
    Adrienne Boswell at Home
    Arbpen Web Site Design Services
    http://www.cavalcade-of-coding.info
    Please respond to the group so others can share
     
    Adrienne Boswell, Jan 23, 2009
    #5
  6. middletree

    middletree Guest

    >
    > HTML ERROR: a numeric is iss not allowed,
    > all id-s should start with a letter.


    This ID is numeric because that's the datatype in the database. I wasn't
    aware that this was incorrect HTML. I will have to go back and look. I might
    be using the "name" attribute instead of the ID.

    >> checked=""checked"""

    >
    > Writing a space & the word 'checked' suffices,


    I think I did this because it's been a practice of mine for years to place
    atrtibutes inside quotes. Doesn't the W3C validator require this for HTML 4
    and XHTML?

    > /> ???
    > Are you writing xml?


    I think this page, which I am writing in Visual Studio, is declared as
    XHTML. I'd be just fine with HTML 4, of course, so maybe I'll go check that
    out. Thanks for pointing me to that.

    As for the code snippet you provided, I was able to use that, with some
    adjustments, and solve this problem. Thanks for your help, and I hope you
    get to feeling better soon.
     
    middletree, Jan 24, 2009
    #6
  7. Gazing into my crystal ball I observed "middletree"
    <> writing in
    news::

    >>
    >> HTML ERROR: a numeric is iss not allowed,
    >> all id-s should start with a letter.

    >
    > This ID is numeric because that's the datatype in the database. I
    > wasn't aware that this was incorrect HTML. I will have to go back and
    > look. I might be using the "name" attribute instead of the ID.
    >


    You could do something like:
    <%for i = 1 to 10%>
    <div id="a_<%=i%>">Something</div>
    <%next%>

    >>> checked=""checked"""

    >>
    >> Writing a space & the word 'checked' suffices,

    >
    > I think I did this because it's been a practice of mine for years to
    > place atrtibutes inside quotes. Doesn't the W3C validator require this
    > for HTML 4 and XHTML?


    HTML is checked, XHTML is checked="checked"

    >
    >> /> ???
    >> Are you writing xml?

    >
    > I think this page, which I am writing in Visual Studio, is declared as
    > XHTML. I'd be just fine with HTML 4, of course, so maybe I'll go check
    > that out. Thanks for pointing me to that.


    Just be sure that which ever one you use you use the correct syntax for
    empty elements, eg: <img> (HTML) <img /> XHTML. Don't mix, and make
    sure there is a DocType, Strict is best.



    --
    Adrienne Boswell at Home
    Arbpen Web Site Design Services
    http://www.cavalcade-of-coding.info
    Please respond to the group so others can share
     
    Adrienne Boswell, Jan 25, 2009
    #7
  8. middletree

    middletree Guest

    > Just be sure that which ever one you use you use the correct syntax
    > for empty elements, eg: <img> (HTML) <img /> XHTML. Don't mix, and
    > make sure there is a DocType, Strict is best.



    I am glad you both pointed this out. As it turns out, I have:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

    "http://www.w3.org/TR/html4/loose.dtd">

    which means I need to go back and make sure that there aren't any stray
    xhtml strings around, like the ones you describe above. My excuse is that
    it's a really old page and I am editing it, but really, there's no excuse
    for this sloppiness.

    Interestingly,. I'm doing most of my testing in IE7, which is still
    displaying it correctly.
     
    middletree, Jan 25, 2009
    #8
  9. Gazing into my crystal ball I observed "middletree"
    <> writing in
    news::


    We're getting off topic here (ms.public.iis.asp.general), so I have set
    follow-ups to alt.html.

    >> Just be sure that which ever one you use you use the correct syntax
    >> for empty elements, eg: <img> (HTML) <img /> XHTML. Don't mix, and
    >> make sure there is a DocType, Strict is best.

    >
    >
    > I am glad you both pointed this out. As it turns out, I have:
    >
    ><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    >
    > "http://www.w3.org/TR/html4/loose.dtd">
    >


    For new pages, you really want strict, for pages that are in transition,
    transitional will do.

    > which means I need to go back and make sure that there aren't any
    > stray xhtml strings around, like the ones you describe above. My
    > excuse is that it's a really old page and I am editing it, but really,
    > there's no excuse for this sloppiness.


    Yes, better to clean them up and use strict.

    >
    > Interestingly,. I'm doing most of my testing in IE7, which is still
    > displaying it correctly.
    >


    I'm sure you know IE7 is buggy. It's better to test in a more compliant
    browser, like Opera or Firefox. Usually, I do my main testing in Opera,
    then if something doesn't display correctly, I debug in Firefox, and
    finally, I _might_ have a look in IE.

    --
    Adrienne Boswell at Home
    Arbpen Web Site Design Services
    http://www.cavalcade-of-coding.info
    Please respond to the group so others can share
     
    Adrienne Boswell, Jan 25, 2009
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    616
  2. Replies:
    1
    Views:
    878
    Jack Klein
    Apr 13, 2005
  3. Adrienne Boswell

    Re: problem with nested loop (I think)

    Adrienne Boswell, Jan 25, 2009, in forum: HTML
    Replies:
    1
    Views:
    369
  4. David Mark
    Replies:
    17
    Views:
    242
    David Mark
    Mar 23, 2010
  5. Isaac Won
    Replies:
    9
    Views:
    388
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page