Re: Form helper issues "Invalid argument supplied for foreach()"

Re: Form helper issues "Invalid argument supplied for foreach()"

am 30.03.2008 12:45:30 von mejpark

On 29 Mar, 22:44, mejpark wrote:
> On 22 Mar, 18:21, Mike Placentra II
>
>
>
> wrote:
> > On Mar 21, 1:32 pm, mejpark wrote:
>
> > > Hello,
>
> > > I'm working my way through O'Reilly's "Learning PHP 5", and I've hit a
> > > brick wall. There are two files involved: the first contains some
> > > helper functions to print HTML forms (form_helpers.php). The second
> > > file (form_meals.php) calls these functions to print an HTML form with
> > > various inputs.
>
> > > The error I'm getting is "( ! ) Warning: Invalid argument supplied for
> > > foreach() in form_helpers.php on line 80". This line is inside the
> > > function input_select, specifically the foreach statement, which is
> > > used to determine which elements are selected by default when the form
> > > is displayed.
>
> > > I think the error relates to the code that calls the input_select
> > > function:
> > > > > > $GLOBALS['main_dishes'], $multiple = true) ?>
> > > This program is taken directly from the ebook, so I cannot see why it
> > > doesn't work.
>
> > > Any pointers much appreciated.
>
> > > Thanks
>
> > It works for me in PHP (cli) 5.2.3 as well as PHP (cgi) 4.4.8. Are you
> > sure that's the exact code you were trying? Could you copy and paste
> > the code from your post and try it again? (there will be some problems
> > that need to be corrected in form_meal.php because when it was posted
> > the lines were word-wrapped so one-line comments were broken into
> > two).
>
> > This is unrelated to your problem, but since you're learning PHP5
> > anyway you can benefit from using...
> > echo "abc", $def, "ghi";
> > ...instead of...
> > print "abc" . $def . "ghi";
> > ...since using echo with commas in PHP5 tells it to output each string
> > consecutively instead of concatenating it and then outputting it all
> > as one (concatenating takes a little extra time, more significant in a
> > loop). This doesn't apply if you're not outputting the string, though,
> > such as when you are returning it. This would be useful for that line
> > in the input_select() function definition (form_helpers.php) in the
> > foreach loop...
> > print '

Re: Form helper issues "Invalid argument supplied for foreach()"

am 30.03.2008 15:47:12 von Jerry Stuckle

mejpark wrote:
> On 29 Mar, 22:44, mejpark wrote:
>> On 22 Mar, 18:21, Mike Placentra II
>>
>>
>>
>> wrote:
>>> On Mar 21, 1:32 pm, mejpark wrote:
>>>> Hello,
>>>> I'm working my way through O'Reilly's "Learning PHP 5", and I've hit a
>>>> brick wall. There are two files involved: the first contains some
>>>> helper functions to print HTML forms (form_helpers.php). The second
>>>> file (form_meals.php) calls these functions to print an HTML form with
>>>> various inputs.
>>>> The error I'm getting is "( ! ) Warning: Invalid argument supplied for
>>>> foreach() in form_helpers.php on line 80". This line is inside the
>>>> function input_select, specifically the foreach statement, which is
>>>> used to determine which elements are selected by default when the form
>>>> is displayed.
>>>> I think the error relates to the code that calls the input_select
>>>> function:
>>>> >>>> $GLOBALS['main_dishes'], $multiple = true) ?>
>>>> This program is taken directly from the ebook, so I cannot see why it
>>>> doesn't work.
>>>> Any pointers much appreciated.
>>>> Thanks
>>> It works for me in PHP (cli) 5.2.3 as well as PHP (cgi) 4.4.8. Are you
>>> sure that's the exact code you were trying? Could you copy and paste
>>> the code from your post and try it again? (there will be some problems
>>> that need to be corrected in form_meal.php because when it was posted
>>> the lines were word-wrapped so one-line comments were broken into
>>> two).
>>> This is unrelated to your problem, but since you're learning PHP5
>>> anyway you can benefit from using...
>>> echo "abc", $def, "ghi";
>>> ...instead of...
>>> print "abc" . $def . "ghi";
>>> ...since using echo with commas in PHP5 tells it to output each string
>>> consecutively instead of concatenating it and then outputting it all
>>> as one (concatenating takes a little extra time, more significant in a
>>> loop). This doesn't apply if you're not outputting the string, though,
>>> such as when you are returning it. This would be useful for that line
>>> in the input_select() function definition (form_helpers.php) in the
>>> foreach loop...
>>> print '

Re: Form helper issues "Invalid argument supplied for foreach()"

am 30.03.2008 17:54:10 von good

Jerry is right this is the sort of problem you really need to see the
exact code for - unfortunately it sounds like a lot of code would be
needed ... So personally I dont want to see it. (I have a life!)

Some things you might want to consider -

The error message is quite specific - it can't find a value.

The sorts of things that could make this happen and may be worth looking
into could be:

1. It was never defined - does it actually exist as spelt in the error
message etc? Check this first - its the mistake I make most often.

2. scoping - this is most likely the problem - particularly in a program
that actually seems to call the $GLOBALS array directly. (Bad practice
IMO)

Inside a function the array defining the lost value needs to be listed eg

function myFunc {
globals thisArrayContainingTheLostValue;

}

If it isnt the function wont find it and thats the error you get.


The practice of using arrays to store arrays is a regular PHP practice
that really should see the programmers taken out and skinned alive IMHO
anyway ... but there you go PHP engenders such daft methods. It isn't
normally a problem until people start thinking they should build entire
applications with it. Then the blindness takes over. Sadly PHP attracts
untrained people (good thing I know) who dont know there are ways of
doing things that were worked out long ago for very good reasons ...
:GOTO myNextPara...

3. Is that array in-scope with the program at the time - was it created
in one file that was jumped from at some point? If the current file was
accessed directly rather than being included in a chain that includes the
array definition - the array could have simply dropped out of scope

4. Has something else cleared/unset that array or variable prior to you
trying to access it?

Scope I feel is key to your problem here... it's something to look at
anyway.

Also double check all your { and } are correct - a subtle mistake with
just one can cause something to shift scope that may not necessarily
cause the program to fail until the scope error is cleared.

Also be sure that foreach() isnt going out of bounds before it reaches
the error

Stay focussed on that error message - its telling you the answer somehow.