473,322 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Displaying checked checkboxes from arrays

Hi all,
This is my first post so please bear with me.

I have a youth soccer uniform order ASP application that loads and pulls from a SQL database that allows soccer teams to construct a uniform kit.

Each piece of the uniform kit can come in a possible 14 different sizes. But some may only come in 12, 6, 3 sizes or whatever.

So when the team uniform guy Creates the kit he checks what sizes are available for each jersey, shorts, socks, etc.
So I set up 14 checkboxes for each Item of clothing on the Kit creation form. (And a kit update page) These are single named sets of check boxes like “KitJersey1Size”

The problem comes with the kit update page; I would like to display all the checkboxes that were checked in the Kit creation form as "Checked" and allow the sizes checked to be changed. And display the checkboxs as"unchecked" if they were not checked originally.

My solution is to create an array of the listed sizes from the data base and then create an array of all the possible 14 sizes. Then nest two for loops to check if the size exists if it does write a checked checkbox, if not write an unchecked checkbox.

My problem is that I can’t get my head around the VBScript syntax for this brilliant idea of mine. Also this may not be the most elegant solution; I strive to write KISS code whenever possible. Am I all wet or what?


So please any help would be really appreciated.
Thanks in advance
Dave
Dec 8 '07 #1
9 2079
jhardman
3,406 Expert 2GB
Dave,

This sounds like a perfectly valid approach, the difficult step here is generally to think up how is the data of "all possible sizes" stored and how is the data of "sizes selected" stored, and how can we get the two to jive. So let me ask, what will these two data sets look like at this point? If you wanted to just print out a list of every size available and every size selected (don't worry yet about making it look good), how would the code look?

Jared
Dec 11 '07 #2
Hi Jared,
Thanks for replying


To print out the 2 sets of sizes

I would:
Expand|Select|Wrap|Line Numbers
  1. 'All Sizes array
  2. AllADSize = "Adult2XS, AdultXS, AdultS, AdultM,    AdultLG, AdultXL, Adult2XL"
  3. ArrAllADSize = split(AllADSize,", ")
  4.  
  5. 'Selected Sizes array from DB
  6. AllJS1 = (rs2("KitJersey1Size"))
  7. arrJ1Size = split(AllJS1,", ")
  8.  
  9.  
  10. for j = 0 to unbound(ArrAllADSize)
  11.  response.Write " AllSizes= " & ArrAllADSize(j) &"<BR>"
  12.  
  13. next
  14.  
  15. 'and
  16.  
  17. for i = 0 to ubound(arrJ1Size)
  18. response.Write " SizesSelected = " & arrJ1Size(j) &"<BR>"
  19. next     
  20.  
  21.  
Here is the code I am currently trying:

Expand|Select|Wrap|Line Numbers
  1. 'Selected Sizes array from DB
  2.  
  3.     AllJS1 = (rs2("KitJersey1Size"))
  4.     'response.Write("AllJS1 = " & AllJS1)
  5.     arrJ1Size = split(AllJS1,", ")
  6.  
  7. 'All sizes array            
  8.  
  9. AllADSize = "Adult2XS, AdultXS, AdultS, AdultM,    AdultLG, AdultXL, Adult2XL"
  10.     'response.Write("AllADSize = " & AllADSize)
  11.     ArrAllADSize = split(AllADSize,", ")
  12.  
  13.  
  14. 'Outer loop of "all sizes"    
  15.  
  16.     for j = 0 to unbound(ArrAllADSize)
  17.          'response.Write " 1st Loop = " '& ArrAllADSize(j) 
  18.  
  19. 'Inner loop of sizes from data base
  20.  
  21.         for i = 0 to ubound(arrJ1Size)
  22.             if  arrJ1Size(i) <> ArrAllADSize(j) then     
  23.  
  24. %>                 
  25.  
  26.         <%=arrJ1Size(i)%><input type="checkbox" name="KitJersey1Size" value="" class="smallplaintext">
  27.  
  28.  
  29.  
  30.             <%
  31.             exit for
  32.  
  33.             elseif  arrJ1Size(i) = ArrAllADSize(j) then
  34.  
  35.             %>    
  36.  
  37.  
  38.             <%=arrJ1Size(i)%><input type="checkbox" name="KitJersey1Size" value="<%=arrJ1Size(i)%>" class="smallplaintext" checked="checked">
  39.  
  40.             <%
  41.  
  42.             end if
  43.             Next
  44.  
  45.             Next
  46.  
  47.             %>
  48.  
Thanks again
I just can't seem to get my head around the syntax of these two loops
Dave
Dec 12 '07 #3
jhardman
3,406 Expert 2GB
Hi Jared,
Thanks for replying


To print out the 2 sets of sizes

I would:
Expand|Select|Wrap|Line Numbers
  1. 'All Sizes array
  2. AllADSize = "Adult2XS, AdultXS, AdultS, AdultM,    AdultLG, AdultXL, Adult2XL"
  3. ArrAllADSize = split(AllADSize,", ")
  4.  
  5. 'Selected Sizes array from DB
  6. AllJS1 = (rs2("KitJersey1Size"))
  7. arrJ1Size = split(AllJS1,", ")
  8.  
  9.  
  10. for j = 0 to unbound(ArrAllADSize)
  11.  response.Write " AllSizes= " & ArrAllADSize(j) &"<BR>"
  12.  
  13. next
  14.  
  15. 'and
  16.  
  17. for i = 0 to ubound(arrJ1Size)
  18. response.Write " SizesSelected = " & arrJ1Size(j) &"<BR>"
  19. next     
Are the two lists necessarily in the same order? I mean the list of all sizes might be "AA, BB, CC, DD" and the selected list might be "BB, DD" and definitely not "DD, CC".

Jared
Dec 13 '07 #4
Hmm... Interesting question,

The all sizes array should be in order because it was loaded in order. I suppose I could load it like this:
Expand|Select|Wrap|Line Numbers
  1. ArrAllADSize(0) = Adult2XS
  2. ArrAllADSize(1) = AdultXS
  3.  
etc.

To be sure of the order.

But I get your point that arrJ1Size() (from the db) could be in the same order but most likely will be missing some elements listed in ArrAllADSize().

My thought was that by using the two loops I would run a comparison with the first element of ArrAllADSize(0-14) with each of the elements in arrJ1Size(0-?) to find a match or not then loop to ArrAllADSize(1) and compare again with each of the elements in arrJ1Size(0-?) and so on...
Since I am comparing all the elements in arrJ1Size(0-?) with each element in ArrAllADSize() would the order matter?
Either Adult2XS (ArrAllADSize(0)) exists in arrJ1Size() or it doesn’t, and so on. Or am I missing something here?

Thanks
Dave
Dec 13 '07 #5
As to the data stored question here is what happend when I printed out both arrays

Here is the allsizes array printout:
AllSizes= Adult2XS
AllSizes= AdultXS
AllSizes= AdultSM
AllSizes= AdultMED
AllSizes= AdultLG
AllSizes= AdultXL
AllSizes= Adult2XL
AllSizes= Youth2XS
AllSizes= YouthXS
AllSizes= YouthSM
AllSizes= YouthMED
AllSizes= YouthLG
AllSizes= YouthXL
AllSizes= Youth2XL

From the database:
AllJS1 = Adult2XS, AdultXS, AdultSM, AdultMED, AdultLG, AdultXL, Adult2XL, Youth2XS, YouthXS, YouthSM, YouthMED, YouthLG, YouthXL, Youth2XL

Then the array printout
SizesSelected = Adult2XS
SizesSelected = AdultXS
SizesSelected = AdultSM
SizesSelected = AdultMED
SizesSelected = AdultLG
SizesSelected = AdultXL
SizesSelected = Adult2XL
SizesSelected = Youth2XS
SizesSelected = YouthXS
SizesSelected = YouthSM
SizesSelected = YouthMED
SizesSelected = YouthLG
SizesSelected = YouthXL
SizesSelected = Youth2XL

this looks good to me
Dave
Dec 13 '07 #6
jhardman
3,406 Expert 2GB
My thought was that by using the two loops I would run a comparison with the first element of ArrAllADSize(0-14) with each of the elements in arrJ1Size(0-?) to find a match or not then loop to ArrAllADSize(1) and compare again with each of the elements in arrJ1Size(0-?) and so on...
Since I am comparing all the elements in arrJ1Size(0-?) with each element in ArrAllADSize() would the order matter?
Either Adult2XS (ArrAllADSize(0)) exists in arrJ1Size() or it doesn’t, and so on. Or am I missing something here?

Thanks
Dave
You could definitely do it that way, and that might be better than what I was going to suggest. I was going to say loop through the bigger list once. within the big list, advance through smaller list only if you get a match:
Expand|Select|Wrap|Line Numbers
  1. for x = 0 to ubound(biglist)
  2.    if biglist(x) = smallList(i) then
  3.       'print out a checked box
  4.       i = i + 1
  5.    else
  6.       'print out an un-checked box
  7.    end if
  8. next
If it is a hundred percent guaranteed that they will be in the same order this will work just fine. Otherwise, your solution would be preferred.

Jared
Dec 14 '07 #7
You could definitely do it that way, and that might be better than what I was going to suggest. I was going to say loop through the bigger list once. within the big list, advance through smaller list only if you get a match:
Expand|Select|Wrap|Line Numbers
  1. for x = 0 to ubound(biglist)
  2.    if biglist(x) = smallList(i) then
  3.       'print out a checked box
  4.       i = i + 1
  5.    else
  6.       'print out an un-checked box
  7.    end if
  8. next
If it is a hundred percent guaranteed that they will be in the same order this will work just fine. Otherwise, your solution would be preferred.

Jared
My method is all well and good but I can't get it to work.
I don't know what I am doing wrong. "exit for" in the wrong place??? I'm stumped
Here is the latest attempt:
Expand|Select|Wrap|Line Numbers
  1.     for j = 0 to ubound(ArrAllADSize)
  2.          'response.Write " 1st Loop = " & ArrAllADSize(j) 
  3.  
  4.         for i = 0 to ubound(arrJ1Size)
  5.             if   arrJ1Size(i) = ArrAllADSize(j)  then     
  6.             'response.Write " 2nd Loop = " & arrJ1Size(1)
  7. %>
  8.         <%=ArrAllADSize(j)%><input type="checkbox" name="KitJersey1Size" value="<%=ArrAllADSize(j)%>" class="smallplaintext" checked="checked">
  9.  
  10.  
  11. <%            exit for
  12.             'next
  13.             elseif  arrJ1Size(i) <> ArrAllADSize(j) then            
  14. %>    
  15.                 <%=ArrAllADSize(j)%><input type="checkbox" name="KitJersey1Size" value="" class="smallplaintext">
  16.  
  17. <%
  18.             end if
  19.             Exit for
  20.             Next
  21.  
  22.             Next
  23.  
  24.  
  25. %>
  26.  
when I run this I get all the checkboxes printed.

In this instance all the sizes are the same in both arrays so all the checkboxes printed should be checked. What I get is the first checkbox is printed and is checked, then all the other checkboxes are printed but, are not checked as they should be.

It's like the first loop fires once then and the test works. Then for some reason the test fails. Is the first loop stoping after the first itteration? I don't know.

Thanks
Dave
Dec 14 '07 #8
jhardman
3,406 Expert 2GB
Dave,

Whenever you use an "exit for" there is quaranteed to be a better solution. Try it more like this:
Expand|Select|Wrap|Line Numbers
  1. for j = 0 to ubound(ArrAllADSize)
  2.     addedText = ""
  3.      'response.Write " 1st Loop = " & ArrAllADSize(j) 
  4.  
  5.     for i = 0 to ubound(arrJ1Size)
  6.         if   arrJ1Size(i) = ArrAllADSize(j)  then     
  7.             addedText = " checked=" & chr(34) & "checked" & chr(34)
  8.         end if
  9.     next %>
  10.         <%=ArrAllADSize(j)%><input type="checkbox" name="KitJersey1Size" value="<%=ArrAllADSize(j)%>" class="smallplaintext" <%=addedText%>>
  11.     <%
  12. next %>
Your "exit for" lines were second guessing you, the inner loop was never moving, it ran through the loop once then exited. If the first value was the same you got a "checked" box, otherwise you always got a "unchecked" box.

Let me know if this helps.

Jared
Dec 17 '07 #9
Hi Jared,

Thanks so much that works perfectly!
I think it is a typical case of me trying to make things more complicated than they need to be. I just couldn't see what I was doing wrong. Thanks again
this is a great group

Dave
Dec 17 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Pete | last post by:
There is a Summary/Example further down... On page one of my site I have a form with some checkboxes and detailed descriptions. When the form is submitted (to page two), the values of the...
4
by: Piotr | last post by:
how can I read (in alert for example) array index number of checked checkbox? I have: <input type="checkbox" id="id_number" name="check" value="1" onclick="show()"/> <input type="checkbox"...
2
by: Aaron Queenan | last post by:
Is there any way to know whether the OnItemCheck is being called in response to a user action (mouse or keyboard) as opposed to the form loading? I have a class which derives from...
4
by: RodBillett | last post by:
I have a situation where I have 3 checkboxes - and at least 1 needs to be selected at all times... I have implemented the code that allows this behavior to happen, BUT Viewstate gets all messed...
1
by: kit7kat | last post by:
hi there, do u have any idea how i can display the names of checkboxes checked from different webpages. Say one page has a list of checkboxes categorized are fruits. Then another page has list...
1
by: Kevin R | last post by:
This is one of the weirdest problems I have ever run into. I have had to trim down a bunch of code to give a sample that is more easily readable by those who will view this. Here is the problem:...
4
by: haresh.amis | last post by:
hello to all, I m using .net 2.0 and i face a problem that is as under Well I have a checkboxlist which i bound in .cs page now I want to count that how many checkboxes ate checked ( In...
2
by: dkultasev | last post by:
Hello, I have small script which generates some listboxes. Their names are listXX (list01, list02, list03....). How to check if there are checked or not ? If I have 1 listbox and have it's name I...
11
by: dba | last post by:
Have been displaying data from database using html for some time but just recently trying to display data back to "form". Can't find answer. <form method="post" action="<?php echo $PHP_SELF;?>">...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.