CodeIgniter Forums
Count returns 1 when empty - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Count returns 1 when empty (/showthread.php?tid=28394)



Count returns 1 when empty - El Forum - 03-10-2010

[eluser]jakeone[/eluser]
Hi

I have a series of checkboxes. I want to see how many are checked, so I do the following:

Code:
$checkboxCount = count($this->input->post("myCheckboxes"));

This returns 1, even when I have not checked any of the boxes (but returns the correct number when I do check some of the boxes)

Maybe this is the wrong way to go about it, I'm not sure.

Any ideas?

Thanks

Jake


Count returns 1 when empty - El Forum - 03-10-2010

[eluser]WanWizard[/eluser]
The method returns FALSE when the variable requested is not present in the array, which is what happens with checkboxes when you don't check any of them:
Code:
$checkboxes = $this->input->post("myCheckboxes");
$checkboxCount = $checkboxes === FALSE ? 0 : count($checkboxes);