1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

I need help with a while statement

Discussion in 'C#' started by MidoriWeb, Dec 5, 2008.

  1. #1
    Hey guys,

    I'm trying to code something but I'm not sure how to do it correctly.

    I have a bunch of products in a database and one of the fields will contain either a "Y" value or something else (like "N" or "NULL").

    On the shopping cart page, I want to display certain text if all the products contain the Y value, and display something else if 1 or more do not contain the Y value.

    I think the best way to go about this is to do a while loop and if it completes display one version of text, and if it doesn't complete all the way through it will display something else.

    But how would I code for that?

    The variable I'm using is:

    ShopCart2002(cartFreeShippingItem,i)

    i is the the number of that item in the shopping cart.

    Any help would be appreciated. I usually do ok at modifying my sites ASP code to my liking by I'm a little lost on this one.
     
    MidoriWeb, Dec 5, 2008 IP
  2. MidoriWeb

    MidoriWeb Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    I'm thinking of something like the code I came up with below. I know it's not right... but can someone clean it up for me and make it workable (or even suggest a better way to go about this)?

    <%
    Dim AllFieldsDisplayY
    AllFieldsDisplayY = "Yes"
    Do While Not ShopCart2002(cartFreeShippingItem,i).EOF
    If ShopCart2002(cartFreeShippingItem,i) <> "Y" Then
    AllFieldsDisplayY = "No"
    Else
    ShopCart2002(cartFreeShippingItem,i).MoveNext
    End If
    %>
    
    <% If AllFieldsDisplayY = "Yes" Then %>
    Yes, all the fields displayed Y.
    <% Else %>
    No, One or more did not display Y.
    <% End If %>
    Code (markup):
     
    MidoriWeb, Dec 5, 2008 IP
  3. MidoriWeb

    MidoriWeb Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #3
    This seems to be working for me:

    <%
    Dim FreeShippingDisplayY
    FreeShippingDisplayY = "Yes"
    For i = 1 to NumCartItems
    If ShopCart2002(cartFreeShippingItem,i) <> "Y" Then
    FreeShippingDisplayY = "No"
    End If
    Next
    %>
    Code (markup):
    Is that a good way to go about this? Did I code everything correctly?
     
    MidoriWeb, Dec 5, 2008 IP
  4. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    I would make the field in the database a Boolean type (true/false) rather then a string.. then just SELECT from the database where the field is false and if it is EOF then there is none...
    e.g
    execute SQL:
    SELECT * FROM cartFreeShippingItem WHERE freeShipping = False

    then:
    IF Recordset.EOF THEN
    FreeShippingDisplayY = "Yes"
    ELSE
    FreeShippingDisplayY = "No"
    END IF

    but if the your method works just stick to that.
     
    camjohnson95, Dec 7, 2008 IP
  5. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    by the way.. the first code you posted probably didn't work because you didn't have a 'Loop' statement at the end (after End If).

    You would also need an 'Exit Loop' (or 'Exit Do' in ASP.NET) after:
    If ShopCart2002(cartFreeShippingItem,i) <> "Y" Then
    AllFieldsDisplayY = "No"

    Otherwise it will be an endless loop as it is not moving to the next record.
     
    camjohnson95, Dec 7, 2008 IP
  6. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #6
    so:
    
    <%
    Dim AllFieldsDisplayY
    AllFieldsDisplayY = "Yes"
    Do While Not ShopCart2002(cartFreeShippingItem,i).EOF
    If ShopCart2002(cartFreeShippingItem,i) <> "Y" Then
    AllFieldsDisplayY = "No"
    Exit Loop
    Else
    ShopCart2002(cartFreeShippingItem,i).MoveNext
    End If
    Loop
    %>
    
    <% If AllFieldsDisplayY = "Yes" Then %>
    Yes, all the fields displayed Y.
    <% Else %>
    No, One or more did not display Y.
    <% End If %>
    
    Code (markup):
     
    camjohnson95, Dec 7, 2008 IP