Jump to content

Warning: Invalid argument supplied for foreach() in


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

That means $this->albums is not an array.  Your code assumes that it is.  This code is...weird.  What kind of tutorial are you following?  Did you do all the other steps up to this point?  Is this a zend framework tutorial?

 

-Dan

This is the tutorial I followed: http://akrabat.com/wp-content/uploads/Getting-Started-with-Zend-Framework.pdf

 

I did recheck all the steps and I'm sure I did it right. But I'm not that sure about my MySQL configuration, could that cause the problem or is it php relevant?

 

-T

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

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

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

  • 2 years later...

None is wrong in the code, it works really good. You have to write all the php files before to run the test; the page 

 

http://localhost/zf-tutorial/public/

 

will not work until you have configured all the files in the project.

 

Go again, and good luck.

 

Enrique. 

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.