twome Posted April 20, 2006 Share Posted April 20, 2006 I have a huge FORM with mostly checkboxes. How can I get the number (count) of checkboxes per group. I'm guessing I would have to using something like count($_POST). So how can I determine the number of checkboxes in the group "TypeN" and then for the group "relationN", etc.For example:<input name="Type1" type="checkbox" id="Type1" value="checked" /><input name="Type2" type="checkbox" id="Type2" value="checked" /><input name="Type3" type="checkbox" id="Type3" value="checked" /><input name="Type4" type="checkbox" id="Type4" value="checked" /><input name="relation1" type="checkbox" id="relation1" value="checked" /><input name="relation2" type="checkbox" id="relation2" value="checked" /><input name="relation3" type="checkbox" id="relation3" value="checked" /><input name="relation4" type="checkbox" id="relation4" value="checked" /><input name="relation5" type="checkbox" id="relation5" value="checked" /> Quote Link to comment https://forums.phpfreaks.com/topic/7921-php-checkbox-count/ Share on other sites More sharing options...
sasa Posted April 20, 2006 Share Posted April 20, 2006 [!--quoteo(post=366722:date=Apr 20 2006, 08:39 AM:name=twome)--][div class=\'quotetop\']QUOTE(twome @ Apr 20 2006, 08:39 AM) [snapback]366722[/snapback][/div][div class=\'quotemain\'][!--quotec--]I have a huge FORM with mostly checkboxes. How can I get the number (count) of checkboxes per group. I'm guessing I would have to using something like count($_POST). So how can I determine the number of checkboxes in the group "TypeN" and then for the group "relationN", etc.For example:<input name="Type1" type="checkbox" id="Type1" value="checked" /><input name="Type2" type="checkbox" id="Type2" value="checked" /><input name="Type3" type="checkbox" id="Type3" value="checked" /><input name="Type4" type="checkbox" id="Type4" value="checked" /><input name="relation1" type="checkbox" id="relation1" value="checked" /><input name="relation2" type="checkbox" id="relation2" value="checked" /><input name="relation3" type="checkbox" id="relation3" value="checked" /><input name="relation4" type="checkbox" id="relation4" value="checked" /><input name="relation5" type="checkbox" id="relation5" value="checked" />[/quote]try[code]<form action="" method="POST">Type <br /><input name="Type[]" type="checkbox" id="Type1" value="1" /><input name="Type[]" type="checkbox" id="Type2" value="2" /><input name="Type[]" type="checkbox" id="Type3" value="3" /><input name="Type[]" type="checkbox" id="Type4" value="4" /><br />relation<br /><input name="relation[]" type="checkbox" id="relation1" value="1" /><input name="relation[]" type="checkbox" id="relation2" value="2" /><input name="relation[]" type="checkbox" id="relation3" value="3" /><input name="relation[]" type="checkbox" id="relation4" value="4" /><input name="relation[]" type="checkbox" id="relation5" value="5" /><br /><input type="submit" name="submit"></form><?phpif (isset($_POST['submit'])){ echo "<pre>"; print_r($_POST); echo "</pre>"; $t = isset($_POST['Type']) ? count($_POST['Type']) : 0; $r = isset($_POST['relation']) ? count($_POST['relation']) : 0; echo "User select $t Type "; if ($t>0){ echo "("; foreach ($_POST['Type'] as $a) echo "Type$a "; echo ") "; } echo "and $r relation checkbox."; if ($r>0){ echo"("; foreach ($_POST['relation'] as $a) echo "relation$a "; echo ") "; } }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/7921-php-checkbox-count/#findComment-28878 Share on other sites More sharing options...
twome Posted April 22, 2006 Author Share Posted April 22, 2006 Actually, I wanted to know not how many boxes the user selected, rather how many boxes there are in that group. (I have a loop that I go through - I need to know how many times to cycle through the loop depending on the number of checkboxes in that group). For TypeN I would return a count of 4 checkboxes (total for that group TypeN). And for relationN I would return 5 checkboxes (total in that group for relationN).For example:<input name="Type1" type="checkbox" id="Type1" value="checked" /><input name="Type2" type="checkbox" id="Type2" value="checked" /><input name="Type3" type="checkbox" id="Type3" value="checked" /><input name="Type4" type="checkbox" id="Type4" value="checked" /><input name="relation1" type="checkbox" id="relation1" value="checked" /><input name="relation2" type="checkbox" id="relation2" value="checked" /><input name="relation3" type="checkbox" id="relation3" value="checked" /><input name="relation4" type="checkbox" id="relation4" value="checked" /><input name="relation5" type="checkbox" id="relation5" value="checked" />Although, I tried your code - works great!!! Thanks! I'll keep that in mind (what you wrote previously) for good reference in case I need it. (I probably will). Quote Link to comment https://forums.phpfreaks.com/topic/7921-php-checkbox-count/#findComment-29620 Share on other sites More sharing options...
kenrbnsn Posted April 22, 2006 Share Posted April 22, 2006 If you uses sasa's code for your form, all you have to do is use the count() on the returned arrays since only those check boxes that are actually checked are returned.Ken Quote Link to comment https://forums.phpfreaks.com/topic/7921-php-checkbox-count/#findComment-29658 Share on other sites More sharing options...
twome Posted April 25, 2006 Author Share Posted April 25, 2006 Ken,I'm not quite sure what you mean by "on the returned arrays". Could you clarify?Thanks,-Coby[!--quoteo(post=367525:date=Apr 22 2006, 04:53 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 22 2006, 04:53 PM) [snapback]367525[/snapback][/div][div class=\'quotemain\'][!--quotec--]If you uses sasa's code for your form, all you have to do is use the count() on the returned arrays since only those check boxes that are actually checked are returned.Ken[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/7921-php-checkbox-count/#findComment-30410 Share on other sites More sharing options...
Barand Posted April 25, 2006 Share Posted April 25, 2006 Use [a href=\"http://www.php.net/foreach\" target=\"_blank\"]foreach()[/a] then you don't need to know how many, you just loop thru however many there are. Quote Link to comment https://forums.phpfreaks.com/topic/7921-php-checkbox-count/#findComment-30778 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.