[英]Invalid argument supplied for foreach() in index.phtml in Zend
The code for index.phtml as follows: index.phtml的代码如下:
<?php foreach ($this->entries as $entry): ?>
<?php echo $this->escape($entry->email) ?>
<br></br>
<?php echo $this->escape($entry->comment) ?>
<?php endforeach ?>
The code for indexAction() in IndexController: IndexController中indexAction()的代码:
public function indexAction()
{
$guestbook = new Application_Model_HRModel();
$view = new Zend_View(array('scriptPath' =>'C:/Users/398853/Documents/NetBeansProjects/PhpProject3/application/views/scripts/index'));
$view->entries = $guestbook->fetchAll();
echo $view->render('index.phtml');
}
The code for fetchAll() in Application_Model_HRModel: Application_Model_HRModel中的fetchAll()的代码:
public function fetchAll()
{
$entry = new Application_Model_HRMo();
$resultSet = $this->getDbTable()->fetchAll();
$entries = array();
foreach ($resultSet as $row) {
$entry->setId($row->id);
$entry->setEmail($row->email);
$entry->setCreated($row->created);
$entry->setComment($row->comment);
$entries[] = $entry;
}
return $entries;
} I have 3 entries ie 3 rows in my databse table . 我在数据库表中有3个条目,即3行。 But When i request the url as http://localhost:8888/Index it says Warning: Invalid argument supplied for foreach() in C:\\Users\\39885Documents\\NetBeansProjects\\PhpProject3\\application\\views\\scripts\\index\\index.phtml on line 54 and then display last row's entry 3 times.I think problem occurs because at first it executes index.phtml and without executing indexAction()(which further executes fetchAll()) $entries will not an array Thats Why at first it gives above stated warning.
但是当我以http:// localhost:8888 / Index请求url时,它说警告:为C:\\ Users \\ 39885Documents \\ NetBeansProjects \\ PhpProject3 \\ application \\ views \\ scripts \\ index \\ index.phtml中的foreach()提供了无效的参数在第54行,然后显示最后一行的条目3次。我认为出现问题是因为首先执行index.phtml而不执行indexAction()(进一步执行fetchAll())$ entries不会是数组,这就是为什么它首先给出上述警告。 Now tell me how to start execution from indexAction() then come at index.phtml so that $entries would be an array.
现在告诉我如何从indexAction()开始执行,然后进入index.phtml,以便$ entries是一个数组。
In fetchAll()
you use the same object, but you should create new in loop. 在
fetchAll()
您使用相同的对象,但应创建新的in循环。 That's why you have 3 duplicated rows in output. 这就是为什么输出中有3个重复的行。 About PHP warning - tru to
var_dump($this->entries)
and check the type 关于PHP警告-tru到
var_dump($this->entries)
并检查类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.