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

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

am 30.03.2008 17:52:29 von George Maicovschi

On Mar 30, 6:16 pm, mejpark wrote:
> On Mar 30, 2:47 pm, Jerry Stuckle wrote:
>
>
>
> > 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 hi=
t a
> > >>>> brick wall. There are two files involved: the first contains some
> > >>>> helper functions to print HTML forms (form_helpers.php). The secon=
d
> > >>>> file (form_meals.php) calls these functions to print an HTML form w=
ith
> > >>>> 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 f=
orm
> > >>>> is displayed.
> > >>>> I think the error relates to the code that calls the input_select
> > >>>> function:
> > >>>> > > >>>> $GLOBALS['main_dishes'], $multiple =3D 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 y=
ou
> > >>> 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 proble=
ms
> > >>> that need to be corrected in form_meal.php because when it was poste=
d
> > >>> 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 stri=
ng
> > >>> 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, thoug=
h,
> > >>> such as when you are returning it. This would be useful for that lin=
e
> > >>> in the input_select() function definition (form_helpers.php) in the
> > >>> foreach loop...
> > >>> print '

';
> } else {
> // No errors? Then $error_text is blank
> $error_text =3D '';...
>
> read more =BB

There is a small catch. The code "if ($_POST['_submit_check'])" is a
wrong approach to detecting a $_POST request, because it will cause an
E_NOTICE message to be displayed on GET requests. You should use "if
($_SERVER['REQUEST_METHOD']=='POST')" instead.