foreach ---- what did I do wrong?

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
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

foreach ---- what did I do wrong?

Post 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?
estamand
Forum Newbie
Posts: 7
Joined: Sat Mar 21, 2009 1:28 pm

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

Post 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");}
 
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

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

Post by becky-atlanta »

I tried and still got the same error.
estamand
Forum Newbie
Posts: 7
Joined: Sat Mar 21, 2009 1:28 pm

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

Post by estamand »

Sure you aren't missing a ; above the for each?

Sorry i can't help you more.
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

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

Post 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.
dead leprachorn
Forum Newbie
Posts: 6
Joined: Tue Mar 31, 2009 10:08 am

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

Post 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
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

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

Post by becky-atlanta »

I suspected the variable created the issue. Thanks for confirming it. I will check/redo the form again and try. :D
estamand
Forum Newbie
Posts: 7
Joined: Sat Mar 21, 2009 1:28 pm

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

Post by estamand »

You can always check to make sure it is array with is_array before your opperation.
User avatar
becky-atlanta
Forum Commoner
Posts: 74
Joined: Thu Feb 26, 2009 6:26 pm
Location: Atlanta, GA

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

Post 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.
estamand
Forum Newbie
Posts: 7
Joined: Sat Mar 21, 2009 1:28 pm

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

Post by estamand »

Glad you figured it out Becky. It always turns out to be something silly.
Post Reply