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

On Mar 30, 6:16 pm, mejpark <mejp... [at] gmail.com> wrote:
> On Mar 30, 2:47 pm, Jerry Stuckle <jstuck... [at] attglobal.net> wrote:
>
>
>
> > mejpark wrote:
> > > On 29 Mar, 22:44, mejpark <mejp... [at] gmail.com> wrote:
> > >> On 22 Mar, 18:21, Mike Placentra II
>
> > >> <nothingsoriginalontheinter... [at] gmail.com> wrote:
> > >>> On Mar 21, 1:32 pm, mejpark <mejp... [at] gmail.com> 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:
> > >>>> <?php input_select('main_dish', $defaults,
> > >>>> $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 '<option value=3D"' . htmlentities($option) . '"';
> > >>> ...which could be rewritten as...
> > >>> echo '<option value=3D"', htmlentities($option), '"';
> > >>> --
> > >>> Michael Placentra II
> > >> It turns out that the problem lies with the array of default values
> > >> named, suprisingly, $defaults. There were no default values for the
> > >> main_dish, which must have caused the error. I managed to get it
> > >> working by adding two selections for the main_dish to the $defaults
> > >> array:
>
> > >> <?php
>
> > >> //right
> > >> $defaults =3D array('delivery' =3D> 'yes',
> > >> 'size' =3D> 'medium',
> > >> 'main_dish' =3D> ar=
ray('cuke', 'tripe',
> > >> 'checkbox' =3D> 'ye=
s'));
>
> > >> var_dump($defaults);
> > >> echo "<br>";
> > >> echo "<br>";
>
> > >> Thanks for the PHP 5 pointers, by the way. Much appreciated!
>
> > > When the script is executed on my production web server, the following=

> > > notices are logged:
> > > PHP Notice: Undefined index: _submit_check in /home/fhlinux150/s/swf=
-
> > > games.net/user/htdocs/portfolio/phpscripts/forms.php on line 48
> > > PHP Notice: Undefined index: _submit_check in /home/fhlinux150/s/swf=
-
> > > games.net/user/htdocs/portfolio/phpscripts/forms.php on line 78
> > > PHP Notice: Undefined index: name in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 8
> > > PHP Notice: Undefined index: sweet in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 73
> > > PHP Notice: Undefined index: puff in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: square in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: cake in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: ricemeat in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: stomach in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: taro in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: giblets in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: abalone in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 78
> > > PHP Notice: Undefined index: comments in /home/fhlinux150/s/swf-
> > > games.net/user/htdocs/portfolio/phpscripts/formhelpers.php on line 32
>
> > > ?
>
> > You never posted the script as requested, so we have no idea what's
> > going on.
>
> > If you want help, you need to post enough information for us to help
> > you. We're not a library or a book store, and very few of us have any
> > specific book.
>
> > --
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> > Remove the "x" from my email address
> > Jerry Stuckle
> > JDS Computer Training Corp.
> > jstuck... [at] attglobal.net
> > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> I've only modified the defaults array--the rest of the code is in
> tact. Here it is:
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> <?php
> /*
> * formhelpers.php
> */
> //print a text box
> function input_text($element_name, $values) {
> print '<input type=3D"text" name=3D"' . $element_name .'" value=3D"';
> print htmlentities($values[$element_name]) . '">';}
>
> //print a submit button
> function input_submit($element_name, $label) {
> print '<input type=3D"submit" name=3D"' . $element_name .'" value=3D"'=
;
> print htmlentities($label) .'"/>';}
>
> //print a textarea
> function input_textarea($element_name, $values) {
> print '<textarea name=3D"' . $element_name .'">';
> print htmlentities($values[$element_name]) . '</textarea>';}
>
> //print a radio button or checkbox
> function input_radiocheck($type, $element_name, $values,
> $element_value) {
> print '<input type=3D"' . $type . '" name=3D"' . $element_name .'"
> value=3D"' . $element_value . '" ';
> if ($element_value =3D=3D $values[$element_name]) {
> print ' checked=3D"checked"';
> }
> print '/>';}
>
> //print a <select> menu
> function input_select($element_name, $selected, $options, $multiple =3D
> false) {
> // print out the <select> tag
> print '<select name=3D"' . $element_name;
> // if multiple choices are permitted, add the multiple attribute
> // and add a [ ] to the end of the tag name
> if ($multiple) { print '[]" multiple=3D"multiple'; }
> print '">';
> // set up the list of things to be selected
> $selected_options =3D array( );
> if ($multiple) {
> foreach ($selected[$element_name] as $val) {
> $selected_options[$val] =3D true;
> }
> } else {
> $selected_options[$selected[$element_name]] =3D true;
> }
> // print out the <option> tags
> foreach ($options as $option =3D> $label) {
> print '<option value=3D"' . htmlentities($option) . '"';
> if ($selected_options[$option]) {
> print ' selected=3D"selected"';
> }
> print '>' . htmlentities($label) . '</option>';
> }
> print '</select>';}
>
> ?>
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> <?php
> /*
> * form_meals.php
> */
> include('formhelpers.php');
> ini_set('display_errors','0');
> // don't forget to include the code for the form
> // helper functions defined in Example 6-29
> // setup the arrays of choices in the select menus
> // these are needed in display_form( ), validate_form( ),
> // and process_form( ), so they are declared in the global scope
> $sweets =3D array('puff' =3D> 'Sesame Seed Puff',
> 'square' =3D> 'Coconut Milk Gelatin Square',
> 'cake' =3D> 'Brown Sugar Cake',
> 'ricemeat' =3D> 'Sweet Rice and Meat');
>
> $main_dishes =3D array('cuke' =3D> 'Braised Sea Cucumber',
> 'stomach' =3D> "Sauteed Pig's Stomach",
> 'tripe' =3D> 'Sauteed Tripe with Wine Sauce',
> 'taro' =3D> 'Stewed Pork with Taro',
> 'giblets' =3D> 'Baked Giblets with Salt',
> 'abalone' =3D> 'Abalone with Marrow and Duck
> Feet');
> // The main page logic:
> // - If the form is submitted, validate and then process or redisplay
> // - If it's not submitted, display
> if ($_POST['_submit_check']) {
> // If validate_form( ) returns errors, pass them to show_form( )
> if ($form_errors =3D validate_form( )) {
> show_form($form_errors);
> } else {
> // The submitted data is valid, so process it
> process_form( );
> }} else {
>
> // The form wasn't submitted, so display
> show_form( );
>
> }
>
> function show_form($errors =3D '') {
> // If the form is submitted, get defaults from submitted
> parameters
> if ($_POST['_submit_check']) {
> $defaults =3D $_POST;
> } else {
> // Otherwise, set our own defaults: medium size and yes to
> delivery
> $defaults =3D array('delivery' =3D> 'yes',
> 'size' =3D> 'medium',
> 'main_dish' =3D> array('=
cuke', 'tripe'),
> 'checkbox' =3D> 'yes');
> }
> // If errors were passed in, put them in $error_text (with HTML
> markup)
> if ($errors) {
> $error_text =3D '<tr><td>You need to correct the following
> errors:';
> $error_text .=3D '</td><td><ul><li>';
> $error_text .=3D implode('</li><li>',$errors);
> $error_text .=3D '</li></ul></td></tr>';
> } 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']=3D=3D'POST')" instead.
George Maicovschi [ So, 30 März 2008 17:52 ] [ ID #1931796 ]
PHP » comp.lang.php » Re: Form helper issues "Invalid argument supplied for foreach()"

Vorheriges Thema: Re: Form helper issues "Invalid argument supplied for foreach()"
Nächstes Thema: Is PDO incompatible with caching mechanismes like APC?