four0four 02-13-2010, 07: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 02-13-2010, 07: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 02-13-2010, 07: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 02-13-2010, 07: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 02-13-2010, 07: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 02-13-2010, 07:57 AM I'm confused as to how I would calculate the number (range) of checkboxes selected. Could you post your HTML?
four0four 02-13-2010, 08: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 02-13-2010, 08: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 02-13-2010, 08: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 02-13-2010, 08: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 02-13-2010, 09: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 02-15-2010, 08:28 PM Thank you all for your help! That helped steer me in the right direction! :)
mlseim 02-15-2010, 08: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).
?>
|