473,549 Members | 2,545 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Displaying checked checkboxes from arrays

6 New Member
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 2089
jhardman
3,406 Recognized Expert Specialist
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
dfletcher
6 New Member
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 Recognized Expert Specialist
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
dfletcher
6 New Member
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
dfletcher
6 New Member
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 Recognized Expert Specialist
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
dfletcher
6 New Member
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 Recognized Expert Specialist
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
dfletcher
6 New Member
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
3955
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 checkboxes are picked up using $_POST and put into session variables. On page two there is another form which is simply a condensed version of the...
4
26961
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" id="id_number" name="check" value="2" onclick="show()"/> <input type="checkbox" id="id_number" name="check" value="3"
2
2999
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 System.Windows.Forms.ListView. Among other features, it enables the checkboxes and overrides the OnItemCheck() method. I have placed this ListView on a form and...
4
2467
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 up. As long as none of the checkboxes are disabled, viewstate works fine - and client actions are 'remembered' after a postback. but when I have...
1
1492
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 of chkboxes categorized as flowers. How can i display all the checked items to only one page? Pls. give me idea. thanks.
1
4096
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: I dynamically add an htmlcheckbox to a webform in the pages render and set the checked value to true. When the page loads, if I remove the check...
4
22837
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 javascript ) and checked checkboxes text will be concat and that string will be pass on to report query I m trying on by this code but it's not work
2
5408
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 do $_POST. But what to do in that situation ? Sincerely, Dmitrij
11
2406
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;?>"> First Name:<input type="text" size="12" maxlength="12" name="Fname"><br > Last Name:<input type="text" size="12" maxlength="36" name="Lname"><br />...
0
7526
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7457
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7723
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7965
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7483
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6051
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3487
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
771
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.