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.
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):
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?
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.
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.
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):