Jump to content

Warning: Invalid argument supplied for foreach() in


Eksotik

Recommended Posts

Hey, I'm new with PHP and I was following some zend -tutorial when this came up:

 

<?php
$this->title = "My Albums";
$this->headTitle($this->title);
?>
<p><a href="<?php echo $this->url(array('controller'=>'index',
'action'=>'add'));?>">Add new album</a></p>
<table>
<tr>
<th>Title</th>
<th>Artist</th>
<th> </th>
</tr>
<?php foreach($this->albums as $album): ?>
<tr>
<td><?php echo $this->escape($album->title);?></td>
<td><?php echo $this->escape($album->artist);?></td>
<td>
<a href="<?php echo $this->url(array('controller'=>'index',
'action'=>'edit', 'id'=>$album->id));?>">Edit</a>

<a href="<?php echo $this->url(array('controller'=>'index',
'action'=>'delete', 'id'=>$album->id));?>">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>

 

I've tried everything but only thing it says is: Warning: Invalid argument supplied for foreach() in index on line 13. Line 13: <?php foreach($this->albums as $album): ?>

 

 

-T

Link to comment
Share on other sites

If you're not fetching data, it won't be available.  Turn on error reporting by putting this line at the top of your script:

error_reporting(E_ALL);

 

Using a framework generally isn't the best way to get started with a programming language, because you're not seeing all the "behind the scenes" things.

 

-Dan

Link to comment
Share on other sites

I tried fetching data and it worked, so I guess MySQL is working.  So I wrote error_reporting(E_ALL); to my index.phtml file but nothing really changed. In php.ini there was this line error_reporting = E_ALL & ~E_DEPRECATED.

 

At start I was just studying basic PHP but somehow I found this cool tutorial. I used some time studying the tutorial and I thought I could try it.. Now I'm pretty confused since everything seems to be right but the app isnt't working..

 

-T

Link to comment
Share on other sites

Unfortunately that's the problem with tutorials like this.  $this is an object-specific variable.  It can ONLY exist inside a function which is inside an object.  You don't know what I'm talking about.  This is because what you're looking at here is a snippet of code that's designed to be sucked into an object function without your knowledge.  Nobody has any chance of figuring out what your code is doing until you post the contents of the object, if you even know where that is.

 

My personal recommendation is to not dive into a framework until you've learned the basics of how PHP works on your own.

 

-Dan

Link to comment
Share on other sites

  • 2 years later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.