I am working on a mailing list application, and I have hit a dead-end that I just cannot comprehend.
I have the following function:
function displayCheckedSubscriptionCheckboxes($cbArray,$name,$checked)
{
//debug stuff
PRINT("<br>Checked Array:<br>");
PRINT_R($checked);
ECHO "<br><br>COUNT = " .count($checked);
PRINT("<br><br>");
//initialize the String to be returned
$str = "";
//if $checked is populated, get checkboxes ready
IF(count($checked) > 0)
{
//$cbArray is an array of checkbox labels gotten from the database
foreach($cbArray as $element)
{
$str .= "<input type = \"checkbox\" name = \"$name\" value = \"$element->id\"";
//$checked is an array of checkboxes checked before.
foreach($checked as $entry)
{
//If the values equal each other - make the checkbox checked already.
IF($entry == $element->id)
{
$str .= " checked ";
}//end if
}//end foreach
$str .= ">$element->name<br>\n";
}//end foreach
}//end if
//if $checked is not populated - just do the regular checkbox without checks in them.
ELSE
{
foreach($cbArray as $element)
{
$str .= "<input type = \"checkbox\" name = \"$name\" value = \"$element->id\">$element->name<br>\n";
}//end foreach
}//end else
}//end function displayCheckedSubscriptionCheckboxes()
PROBLEM:
If the array is empty, it gives the following error:
Warning: Invalid argument supplied for foreach() in /home/thechorale/www/admin/_mailingListItem.php on line 1107
At first, it was because I was not checking to see if the array was set, then I put in the check IF(count($checked) > 0). This check should catch if the array is empty (meaning there was no checkboxes checked previously). BUT......
It still gives that error.
SO.... I tried some debugging stuff and found that the array (eventhough empty) when passed to the count function - gives a 1. No matter what it returns a 1.
Could someone help me figure out why?