invalid foreach argument

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
ziggi07
Forum Newbie
Posts: 2
Joined: Wed May 13, 2009 6:36 am

invalid foreach argument

Post by ziggi07 »

Hey Everyone,

First time user and poster.

Downloaded a joomla blog pluggin and received the following invalid foreach() statement:
Warning: Invalid argument supplied for foreach() in /home/fhlinux161/f/fluidtestsite.co.uk/user/htdocs/components/com_idoblog/assets/templates/idobloger/comment.php on line 2
The first few lines of code are as follows:

Code: Select all

 <?php
foreach ($this->comments as $comment)
}
 
?>
<a name="comment<?php echo $comment->id; ?>"></a><table cellpadding="0" cellspacing="0" border="0" width="100%" ><tr height="8"><td width="<?php echo $comment->indent; ?>"></td><td ><div style="margin:10px;"> 
I'm new to php, but I don't think an argument has been supplied?

Any suggestions?
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: invalid foreach argument

Post by divito »

May not be the full issue you're experiencing but you need to add a curly brace to line 2. Any code to be executed during the foreach needs to go in between them (assuming there will be any), and there was only a closing curly brace in what you pasted.

Like so:

Code: Select all

<?php
foreach ($this->comments as $comment) {
        //code would go here
}
 
?>
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: invalid foreach argument

Post by crazycoders »

The problem is simply that the $this->comments is not an iteratable object. Probably because it was not set, or maybe because of an error in the pluggin... You have to report this to the pluggin developper!
ziggi07
Forum Newbie
Posts: 2
Joined: Wed May 13, 2009 6:36 am

Re: invalid foreach argument

Post by ziggi07 »

I have reported it to the developers, but I'd still like it to work if I can. I've tried.
echo $comment; and print $comment;


neither have worked, but could it be something similar?
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: invalid foreach argument

Post by crazycoders »

It all depends where the $comment is located. It could be a __get magic function that returns this info, or it could be a property of an object. Either way, you first need to learn that something that is not set will get set to '' or NULL if you use it.

Doing echo $comments return nothing because you just set the value that probably did not exist.

What you need to do is either:

var_dump($this->comments) or var_dump($objectname->comments) to be able to display the real current value of the comments property!
Post Reply