The code for index.phtml as follows:
<?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:
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:
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 i.e. 3 rows in my databse table . 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. Now tell me how to start execution from indexAction() then come at index.phtml so that $entries would be an array.