Jump to content


Photo

PHP & Checkbox Count


  • Please log in to reply
5 replies to this topic

#1 twome

twome
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 20 April 2006 - 06:39 AM

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" />



#2 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 20 April 2006 - 08:09 AM

[!--quoteo(post=366722:date=Apr 20 2006, 08:39 AM:name=twome)--][div class=\'quotetop\']QUOTE(twome @ Apr 20 2006, 08:39 AM) View Post[/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
<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>

<?php
if (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 ") ";
    }
    
}
?>


#3 twome

twome
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 22 April 2006 - 05:07 PM

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).




#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 22 April 2006 - 09:53 PM

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

#5 twome

twome
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 25 April 2006 - 05:49 AM

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) View Post[/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]


#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 25 April 2006 - 11:36 PM

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.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users