Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Return number based on how many checkboxes are selected

1. ## Return number based on how many checkboxes are selected

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!

• 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?

• ## Users who have thanked abduraooft for this post:

four0four (02-15-2010)

• Originally Posted by abduraooft
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.

• Use simple if-else-if, like
PHP Code:
``` if(\$count<=5) //echoelseif(\$count>5 && \$count<=10) //echoelseif(\$count>10 && \$count<=15)//echo//and so on  ```

• ## Users who have thanked abduraooft for this post:

four0four (02-15-2010)

• Originally Posted by abduraooft
Use simple if-else-if, like
PHP Code:
``` 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!

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

• ## Users who have thanked abduraooft for this post:

four0four (02-15-2010)

• So far, I just have the basics:

Code:
```<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.

• Code:
```<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:

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

• ## Users who have thanked MattF for this post:

four0four (02-15-2010)

• If you follow MattF's code, you may even avoid all integer indexes from the array notation checkbox[], like
Code:
```<input type="checkbox" name="checkbox[]">
....................```

• ## Users who have thanked abduraooft for this post:

four0four (02-15-2010)

• Originally Posted by abduraooft
If you follow MattF's code, you may even avoid all integer indexes from the array notation checkbox[], like
Code:
```<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.

• Here is something to work with. Untested and might have syntax errors.

PHP Code:
``` \$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++; }  ```

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

• ## Users who have thanked Len Whistler for this post:

four0four (02-15-2010)

• Thank you all for your help! That helped steer me in the right direction!

• Len,

I think you can also do it like this ...
PHP Code:
``` <?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).?> ```

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•