Page 1 of 1

foreach function error

Posted: Thu Dec 11, 2008 11:10 am
by kabucek
hello @LL,


We have registration system and it was working on ok up till now.
When someone tries to register for our event there is an error message:


warning: invalid argument supplied for foreach() in /home/dir1/dir2/indexReg.php on line 5400

the code from line is:

foreach ($classSelectionArray as $classID => $selectionStatus)


and there is another message:

processTemplateArray: input is not a record array


Our php framework is hosted on webhosting company which has centos.
Is it possible that they run some update for php services which caused
bad interactions with "foreach" function? maybe older version of php or
some respository of it handles this differently ?

thanks

Re: foreach function error

Posted: Thu Dec 11, 2008 11:23 am
by John Cartwright
Sounds like they turned error reporting onto me, so the problem was mearly hidden. Hard to tell without seeing more, otherwise.

Generally, you should always check your array has atleast 1 row (and is in fact an array) before you iterate it.

isset() and/or count()

Re: foreach function error

Posted: Thu Dec 11, 2008 12:27 pm
by kabucek
i digged into the changes and revert old change and now I got similar error but for next lines:



Warning: Invalid argument supplied for foreach() in /home/dir1/dir2/indexRegEG.php on line 10833


code:

foreach ($tmpPeriodArray as $label=> $itemArray)


any suggestions?

Re: foreach function error

Posted: Thu Dec 11, 2008 1:10 pm
by Eran
This is the error:
Warning: Invalid argument supplied for foreach()
This is the code:
foreach ($tmpPeriodArray as $label=> $itemArray)
Obviously an invalid argument is supplied for the foreach loop? try checking the contents of $tmpPeriodArray

Code: Select all

var_dump($tmpPeriodArray);

Re: foreach function error

Posted: Thu Dec 11, 2008 4:22 pm
by kabucek
i did that and there is "NULL" right before the error message.
any suggestions now?
thanks

Re: foreach function error

Posted: Thu Dec 11, 2008 4:41 pm
by Eran
yep, a couple:
1. find out why the array is actually a NULL value (is it the result of a database query? maybe the query returns no results or is unsuccessful?)
2. Add a check before the foreach loop to make sure a NULL value doesn't get through:

Code: Select all

if(is_array($tmpPeriodArray) ) {
    // Process array in foreach loop
}

Re: foreach function error

Posted: Thu Dec 11, 2008 4:47 pm
by kabucek
ok,
but how can i found out that ?
thanks