473,406 Members | 2,816 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,406 software developers and data experts.

Adding up combinations with select-multiple boxes

Hi guys,

I'm trying to figure out what bone headed mistake I made on something I
put together. I've got a form (named 'context') that has a variable
number of select-multiple inputs on it. Based on the number of
variables passed through a GET string, I want to multiply the total
number of selected items for each together to see how many possible
combinations the selected items are generating.

The following snippet of code succesfully gets the values from the GET
statement to count the number of items being passed (I know, it assumes
the variable will always be in the same place, I'll deal with that
later), then it's supposed to loop through all elements in the page.
If the item it finds is a select-multiple input (up to this point, it
works. It finds each select-multiple and alerts in debugging), it's
supposed to do a loop inside that element to count how many are
checked, then multiply it against the running total (which is named
predictioncount). I expect the number for predictioncount to be 0
until the last field has an entry in it, but it never stops being
anything BUT zero.

I'm no Javascript expert, and my kludgy code shows it, but I think I'm
close to this functioning and I'm hoping someone here will see and say
"Oh, you got the standard whatsit confused with the thingy, just change
character x to y and it should work".

Thanks in advance for any assistance!

function trackselections()
{
//We'll get the variable passed, then get the name of it. If the item
is selected, we add one to the item. If not, we subtract it
//This lets us map out what will be submitted and calculate complexity
based on that.
var num = document.context.length;
var count=0;
var predictioncount = 0;
var testunit_count = 0;
//First, count how many testcase_ids are passed.
var getstring = location.search.substr(1);
var testunit_ids = 0;
count = getstring.search(/\&/);
getstring = getstring.substr(13,count-13);
testunit_ids = getstring.split(/%2C/g);
testunit_count = testunit_ids.length;

//Next, count all inputs of of the type select-multiple and loop
through them,
//collecting info on how many items are checked

for (var i = 0; i < num; i++)
{
if (document.context[i].type == 'select-multiple')
{
count = 0;
for (var o = 0; o< document.context[i].length; o++)
{
if (document.context[i].id[o].selected) count++;
alert(count);
}
predictioncount = predictioncount * count;
}
alert(predictioncount);
}

}

Jul 23 '05 #1
6 4258
"Ben Hallert" <be*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi guys,

I'm trying to figure out what bone headed mistake I made on something I
put together. I've got a form (named 'context') that has a variable
number of select-multiple inputs on it. Based on the number of
variables passed through a GET string, I want to multiply the total
number of selected items for each together to see how many possible
combinations the selected items are generating.

The following snippet of code succesfully gets the values from the GET
statement to count the number of items being passed (I know, it assumes
the variable will always be in the same place, I'll deal with that
later), then it's supposed to loop through all elements in the page.
If the item it finds is a select-multiple input (up to this point, it
works. It finds each select-multiple and alerts in debugging), it's
supposed to do a loop inside that element to count how many are
checked, then multiply it against the running total (which is named
predictioncount). I expect the number for predictioncount to be 0
until the last field has an entry in it, but it never stops being
anything BUT zero.

I'm no Javascript expert, and my kludgy code shows it, but I think I'm
close to this functioning and I'm hoping someone here will see and say
"Oh, you got the standard whatsit confused with the thingy, just change
character x to y and it should work".

Thanks in advance for any assistance!

function trackselections()
{
//We'll get the variable passed, then get the name of it. If the item
is selected, we add one to the item. If not, we subtract it
//This lets us map out what will be submitted and calculate complexity
based on that.
var num = document.context.length;
var count=0;
var predictioncount = 0;
var testunit_count = 0;
//First, count how many testcase_ids are passed.
var getstring = location.search.substr(1);
var testunit_ids = 0;
count = getstring.search(/\&/);
getstring = getstring.substr(13,count-13);
testunit_ids = getstring.split(/%2C/g);
testunit_count = testunit_ids.length;

//Next, count all inputs of of the type select-multiple and loop
through them,
//collecting info on how many items are checked

for (var i = 0; i < num; i++)
{
if (document.context[i].type == 'select-multiple')
{
count = 0;
for (var o = 0; o< document.context[i].length; o++)
{
if (document.context[i].id[o].selected) count++;
alert(count);
}
predictioncount = predictioncount * count;
}
alert(predictioncount);
}

}


I think you want to replace
if (document.context[i].id[o].selected) count++;
with this
if (document.context[i].options[o].selected) count++;

I don't think you want this:
predictioncount = predictioncount * count;
don't you mean
predictioncount = predictioncount +count;
or better yet:
predictioncount += count;

Jul 23 '05 #2
Lee
Ben Hallert said:

It finds each select-multiple and alerts in debugging), it's
supposed to do a loop inside that element to count how many are
checked, then multiply it against the running total (which is named
predictioncount). I expect the number for predictioncount to be 0
until the last field has an entry in it, but it never stops being
anything BUT zero.


If you start with zero, and keep multiplying that value by any
other numbers, you will always end up with zero.

If I understand you correctly, you want to count the number of
selected items and THEN multiply that number by the number of arguments.

Jul 23 '05 #3
Ben Hallert wrote:
Hi guys,

I'm trying to figure out what bone headed mistake I made on something I put together. I've got a form (named 'context') that has a variable
number of select-multiple inputs on it. Based on the number of
variables passed through a GET string, I want to multiply the total
number of selected items for each together to see how many possible
combinations the selected items are generating.

The following snippet of code succesfully gets the values from the GET statement to count the number of items being passed (I know, it assumes the variable will always be in the same place, I'll deal with that
later), then it's supposed to loop through all elements in the page.
If the item it finds is a select-multiple input (up to this point, it
works. It finds each select-multiple and alerts in debugging), it's
supposed to do a loop inside that element to count how many are
checked, then multiply it against the running total (which is named
predictioncount). I expect the number for predictioncount to be 0
until the last field has an entry in it, but it never stops being
anything BUT zero.

I'm no Javascript expert, and my kludgy code shows it, but I think I'm close to this functioning and I'm hoping someone here will see and say "Oh, you got the standard whatsit confused with the thingy, just change character x to y and it should work".

Thanks in advance for any assistance!

function trackselections()
{
//We'll get the variable passed, then get the name of it. If the item is selected, we add one to the item. If not, we subtract it
//This lets us map out what will be submitted and calculate complexity based on that.
var num = document.context.length;
var count=0;
var predictioncount = 0;
var testunit_count = 0;
//First, count how many testcase_ids are passed.
var getstring = location.search.substr(1);
var testunit_ids = 0;
count = getstring.search(/\&/);
getstring = getstring.substr(13,count-13);
testunit_ids = getstring.split(/%2C/g);
testunit_count = testunit_ids.length;

//Next, count all inputs of of the type select-multiple and loop
through them,
//collecting info on how many items are checked

for (var i = 0; i < num; i++)
{
if (document.context[i].type == 'select-multiple')
{
count = 0;
for (var o = 0; o< document.context[i].length; o++)
{
if (document.context[i].id[o].selected) count++;
alert(count);
}
predictioncount = predictioncount * count;
}
alert(predictioncount);
}

}


I may be missing your point - but how can you read the submitted data
from the querystring until you've submitted the form? Why even bother,
as you can analyze it programmatically without performing any
particular action.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>untitled</title>
<script type="text/javascript">
//<![CDATA[

function getCombos()
{
var s = 0, sel, sels = document.getElementsByTagName('select'),
o, opt, opts,
nsel, predictioncount = 1;
while (sel = sels.item(s++))
{
if (/multiple/.test(sel.type))
{
opts = sel.getElementsByTagName('option');
o = 0;
nsel = 0;
while (opt = opts.item(o++))
if (opt.selected)
++nsel;
}
predictioncount *= nsel || 1;
}
return predictioncount;
}

//]]>
</script>
</head>
<body>
<form style="width:124px;">
<select size="5" multiple="multiple">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<select size="5" multiple="multiple">
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<select size="5" multiple="multiple">
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
</select>
<hr />
<input type="button" value="get combos" onclick="alert(getCombos())" />
</form>
</body>
</html>

Jul 23 '05 #4
Good point on the starting from zero, I'll fix that immediately. I was
trying to do it all on the fly instead of storing my values in an array
because I felt I'd avoid some trouble, but that may not have been the
case.

Jul 23 '05 #5
The reason I was doing predictioncount = predictioncount * count is
because each time it counts up the selected items, I wanted it to
multiply the whole number by that. Eg, if I have 3 variables in the
GET input, and 4 mutliple-select fields that each have two selected,
then the total number of combinations (that I was trying to predict)
would be 3*2*2*2*2=48 total units created. If Iadd them the way you
showed, it would only be tallying the total number of selected items,
not showing the multiplied total.

Eureka! I applied your suggestion (changing id[o] to options[o]) and
changed my initial value for predictioncount to be testunit_ids.length
(to get the number of items passed in the GET variable string I was
looking at) and it works now! Thank you, McKirahan, RobB, and Lee for
your help, it got me over the little hump I was stuck at.
Regards,

Ben

Jul 23 '05 #6
As a followup, here's the working code. It's might not be as elegant
as RobB's, but it might be of use to some faceless denizen of the
future who stumbles into a similar problem while learning JS:

function trackselections()
{
//We'll get the variable passed, then get the name of it. If the item
is selected, we add one to the item. If not, we subtract it
//This lets us map out what will be submitted and calculate complexity
based on that.
var num = document.context.length;
var count=0;
var predictioncount;
var testunit_count = 0;
//First, count how many testcase_ids are passed.
var getstring = location.search.substr(1);
var testunit_ids = 0;
count = getstring.search(/\&/);
getstring = getstring.substr(13,count-13);
testunit_ids = getstring.split(/%2C/g);
predictioncount = testunit_ids.length;
//Next, count all inputs of of the type select-multiple and loop
through them,
//collecting info on how many items are checked

for (var i = 0; i < num; i++)
{
if (document.context[i].type == 'select-multiple')
{
count = 0;
for (var o = 0; o< document.context[i].length; o++)
{
if (document.context[i].options[o].selected) count++;
}
predictioncount = predictioncount * count;
}
}

//If the total number of test units being created is
//over 1000 or so, alert with a warning
if(predictioncount >=500 && predictioncount <=1000)
{
alert("If you submit this, you will create " + predictioncount + "
testunits. Consider making assignments in smaller groups.");
}
if(predictioncount >=1001 && predictioncount <=3000)
{
alert("If you submit this, you will create " + predictioncount + "
testunits. This may take a while (best case scenario) or crash your
browser (worst case scenario). Consider making assignments in smaller
groups.");
}
if(predictioncount >=3001)
{
alert("If you submit this, you will be asking the server to create "
+ predictioncount + " testunits. While the finely crafted swiss
worksmanship of the G5 that serves Toro is ready and willing for the
challenge, your browser most certainly isn't. You _will_ break your
user session, and to add insult to injury, the mind bending quantity of
work you've attempted to create will fall backwards into the shapeless
ether from which it came. Give a hoot, don't pollute.");
}

}

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jim Hubbard | last post by:
Does anyone have any code for doing combinations in VB.net or visual basic ? I'd like to be able to get an array of combinations (not permutations) of words from a base group of words while...
36
by: rbt | last post by:
Say I have a list that has 3 letters in it: I want to print all the possible 4 digit combinations of those 3 letters: 4^3 = 64 aaaa
3
by: Jonathan | last post by:
Hi all! For a match schedule I would like to find all possible combinations of teams playing home and away (without teams playing to themselves of course). I now the simple version works...
5
by: p175 | last post by:
Hi people, Given I have a string record say "A+B+C+D+E", is it possible to parse this string using SQL in DB2 and return all of the possible combinations from the 5 elements to a min length of 3...
3
by: Ryan | last post by:
I've been trying to find an algorithm that will output all of the possible combinations of items in an array. I know how to find the number of combinations for each set using nCr=n!/(r!(n-r)!) ...
1
by: brendan | last post by:
Hi, I'm not at all competent in ms-sql, nor vb, as we work in Oracle and Mysql .... however, we need to port a couple of db queries to ms-access (2000) and I'm having a heck of a time trying to...
7
by: Miro | last post by:
Im a VB Newbie so I hope I'm going about this in the right direction. I have a simple DB that has 1 Table called DBVersion and in that table the column is CurVersion ( String ) Im trying to...
5
by: Leah Trahan | last post by:
I've got a query that returns 8 fields, 7 of which I need to search for in various combinations and the other 1 (Cost) just to view its contents. Below is the mess I've made of it so far: SELECT...
2
by: The Frog | last post by:
Hello everyone, I am trying to find way of writing an SQL query that can produce missing record combinations across a many to many type setup in Access. The three tables used are as follows:...
6
by: jackal_on_work | last post by:
Hi Faculties, I have two queries which give me the same output. -- Query 1 SELECT prod.name, cat.name FROM products prod INNER JOIN categories cat ON prod.category_id = cat.id WHERE cat.id...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.