PDA

View Full Version : Return number based on how many checkboxes are selected

four0four
Feb 13th, 2010, 08:07 AM
Is it possible to return a number based on the number (or range) of checkboxes that are selected?

For example, if I have 20 checkboxes and only 10 are selected, it would echo "5". If all 20 checkboxes are selected it would echo "10".

Thanks!

abduraooft
Feb 13th, 2010, 08:16 AM
For example, if I have 20 checkboxes and only 10 are selected, it would echo "5". If all 20 checkboxes are selected it would echo "10".

Is your logic to echo the half of the selected items?

four0four
Feb 13th, 2010, 08:29 AM
Is your logic to echo the half of the selected items?

Yes, that's correct. Not exactly half all the time, but if I could "specify" a number to echo based on the range of checkboxes selected.

Something like:

If 5-10 checkboxes are selected echo a variable called \$number1.

If 10-15 checkboxes are selected echo a variable called \$number2.

abduraooft
Feb 13th, 2010, 08:36 AM
Use simple if-else-if, like
if(\$count<=5)
//echo
elseif(\$count>5 && \$count<=10)
//echo
elseif(\$count>10 && \$count<=15)
//echo
//and so on

four0four
Feb 13th, 2010, 08:47 AM
Use simple if-else-if, like
if(\$count<=5)
//echo
elseif(\$count>5 && \$count<=10)
//echo
elseif(\$count>10 && \$count<=15)
//echo
//and so on

What is \$count?

I'm confused as to how I would calculate the number (range) of checkboxes selected.

Thanks again!

abduraooft
Feb 13th, 2010, 08:57 AM
I'm confused as to how I would calculate the number (range) of checkboxes selected. Could you post your HTML?

four0four
Feb 13th, 2010, 09:10 AM
So far, I just have the basics:

<input type="checkbox" name="checkbox1">
<input type="checkbox" name="checkbox2">
<input type="checkbox" name="checkbox3">
<input type="checkbox" name="checkbox4">
<input type="checkbox" name="checkbox5">
<input type="checkbox" name="checkbox6">
<input type="checkbox" name="checkbox7">
<input type="checkbox" name="checkbox8">
<input type="checkbox" name="checkbox9">
<input type="checkbox" name="checkbox10">

To make things simple, I just have 10 checkboxes. Let's say only 5 of those checkboxes (in any order) are selected, it would echo a variable called \$number1.

If say, 8 of those checkboxes (in any order) are selected, it would echo a variable called \$number2.

...and so on.

I hope that makes sense. :)

MattF
Feb 13th, 2010, 09:22 AM
<input type="checkbox" name="checkbox[1]">
<input type="checkbox" name="checkbox[2]">
<input type="checkbox" name="checkbox[3]">
<input type="checkbox" name="checkbox[4]">
<input type="checkbox" name="checkbox[5]">
<input type="checkbox" name="checkbox[6]">
<input type="checkbox" name="checkbox[7]">
<input type="checkbox" name="checkbox[8]">
<input type="checkbox" name="checkbox[9]">
<input type="checkbox" name="checkbox[10]">

Then, in the processing section:

\$count = ((isset(\$_POST['checkbox']) && !empty(\$_POST['checkbox'])) ? count(\$_POST['checkbox']) : 0);

abduraooft
Feb 13th, 2010, 09:25 AM
If you follow MattF's code, you may even avoid all integer indexes from the array notation checkbox[], like

<input type="checkbox" name="checkbox[]">
....................

MattF
Feb 13th, 2010, 09:34 AM
If you follow MattF's code, you may even avoid all integer indexes from the array notation checkbox[], like

<input type="checkbox" name="checkbox[]">
....................

God knows why, but I do that same trick every time with html arrays. Never with php arrays, just html. I will stop numbering them one day. :D

Len Whistler
Feb 13th, 2010, 10:10 AM
Here is something to work with. Untested and might have syntax errors.

\$post_result = \$_POST['checkbox'];

\$counter = 1;
foreach (\$post_result as \$value) {
echo "\$value";
\$counter++;
}

echo "Total checkboxes checked: \$counter";

// Below is part of form to be posted to code above.

\$num_checkboxes = 20;
\$counter = 1;

while (\$counter <= \$num_checkboxes) {
echo "<input type=\"checkbox\" name=\"checkbox[]\">";
\$counter++;
}

---------------

four0four
Feb 15th, 2010, 09:28 PM
Thank you all for your help! That helped steer me in the right direction! :)

mlseim
Feb 15th, 2010, 09:39 PM
Len,

I think you can also do it like this ...

<?php
// this will tell you how many boxes were checked ...
\$count=count(\$_POST['checkbox']);

echo \$count;

// this works because only CHECKED checkboxes will be in the array.
// all unchecked checkboxes won't be counted (they do not exist).

?>