foreach function error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kabucek
Forum Commoner
Posts: 50
Joined: Thu Sep 04, 2008 2:20 pm

foreach function error

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: foreach function error

Post 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()
kabucek
Forum Commoner
Posts: 50
Joined: Thu Sep 04, 2008 2:20 pm

Re: foreach function error

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: foreach function error

Post 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);
kabucek
Forum Commoner
Posts: 50
Joined: Thu Sep 04, 2008 2:20 pm

Re: foreach function error

Post by kabucek »

i did that and there is "NULL" right before the error message.
any suggestions now?
thanks
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: foreach function error

Post 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
}
kabucek
Forum Commoner
Posts: 50
Joined: Thu Sep 04, 2008 2:20 pm

Re: foreach function error

Post by kabucek »

ok,
but how can i found out that ?
thanks
Post Reply