Page 1 of 1

foreach ---- what did I do wrong?

Posted: Tue Mar 31, 2009 1:16 pm
by becky-atlanta
I am getting ...Invalid argument supplied for foreach()...

Code: Select all

foreach ($_POST["week"] as $wk) {
fwrite($fh, $wk);
fwrite($fh, "<br>\n");}
:?: :?: :?: :?: :?: I was using the same code in another page and it was working fine.

Any advice?

Re: foreach ---- what did I do wrong?

Posted: Tue Mar 31, 2009 1:48 pm
by estamand
Have you tried to use single quotes for your $_POST?

Code: Select all

 
   1. foreach ([color=#BF0000]$_POST['week'][/color] as $wk) {
   2. fwrite($fh, $wk);
   3. fwrite($fh, "<br>\n");}
 

Re: foreach ---- what did I do wrong?

Posted: Tue Mar 31, 2009 1:58 pm
by becky-atlanta
I tried and still got the same error.

Re: foreach ---- what did I do wrong?

Posted: Tue Mar 31, 2009 2:19 pm
by estamand
Sure you aren't missing a ; above the for each?

Sorry i can't help you more.

Re: foreach ---- what did I do wrong?

Posted: Tue Mar 31, 2009 2:38 pm
by becky-atlanta
Thanks for trying. I have been checking it, and checking it, and checking it for over a day and have not found out why it is not working. Like I said, it is working fine on another page.

Re: foreach ---- what did I do wrong?

Posted: Tue Mar 31, 2009 2:47 pm
by dead leprachorn
the "error invalid argument supplied" suggests you are not performing foreach() on an array. Have you checked that you are referencing the correct variable | sending the data properly? a null value will result in an invalid argument supplied error.

Code: Select all

 
<?php
 foreach($empty as $empt) {
 
 
}
?>
 
will result in..

Code: Select all

Warning: Invalid argument supplied for foreach() in /.../error.php on line 2

Re: foreach ---- what did I do wrong?

Posted: Tue Mar 31, 2009 2:56 pm
by becky-atlanta
I suspected the variable created the issue. Thanks for confirming it. I will check/redo the form again and try. :D

Re: foreach ---- what did I do wrong?

Posted: Tue Mar 31, 2009 3:26 pm
by estamand
You can always check to make sure it is array with is_array before your opperation.

Re: foreach ---- what did I do wrong?

Posted: Tue Mar 31, 2009 9:41 pm
by becky-atlanta
Since you reply, I'll tell you what I did wrong.

I had this in the code and that messed things up.
<?php echo $PHP_SELF ?>
After I changed it to
<?php $PHP_SELF ?>
everything works fine.

:oops: Now, I will always remember it.

Re: foreach ---- what did I do wrong?

Posted: Tue Mar 31, 2009 10:56 pm
by estamand
Glad you figured it out Becky. It always turns out to be something silly.