Eksotik Posted February 7, 2011 Share Posted February 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/226952-warning-invalid-argument-supplied-for-foreach-in/ Share on other sites More sharing options...
ManiacDan Posted February 7, 2011 Share Posted February 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/226952-warning-invalid-argument-supplied-for-foreach-in/#findComment-1170966 Share on other sites More sharing options...
Eksotik Posted February 7, 2011 Author Share Posted February 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/226952-warning-invalid-argument-supplied-for-foreach-in/#findComment-1170973 Share on other sites More sharing options...
ManiacDan Posted February 7, 2011 Share Posted February 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/226952-warning-invalid-argument-supplied-for-foreach-in/#findComment-1171027 Share on other sites More sharing options...
Eksotik Posted February 7, 2011 Author Share Posted February 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/226952-warning-invalid-argument-supplied-for-foreach-in/#findComment-1171090 Share on other sites More sharing options...
ManiacDan Posted February 7, 2011 Share Posted February 7, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/226952-warning-invalid-argument-supplied-for-foreach-in/#findComment-1171103 Share on other sites More sharing options...
Eksotik Posted February 7, 2011 Author Share Posted February 7, 2011 Oh, well.. I think I go back to basics. Anyway, thanks for your help, Dan! -T Quote Link to comment https://forums.phpfreaks.com/topic/226952-warning-invalid-argument-supplied-for-foreach-in/#findComment-1171118 Share on other sites More sharing options...
cvenzos Posted April 11, 2013 Share Posted April 11, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/226952-warning-invalid-argument-supplied-for-foreach-in/#findComment-1424149 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.