Page 1 of 1

invalid foreach argument

Posted: Wed May 13, 2009 6:49 am
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?

Re: invalid foreach argument

Posted: Wed May 13, 2009 6:57 am
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
}
 
?>

Re: invalid foreach argument

Posted: Wed May 13, 2009 11:53 am
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!

Re: invalid foreach argument

Posted: Thu May 14, 2009 9:25 am
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?

Re: invalid foreach argument

Posted: Thu May 14, 2009 9:30 am
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!