Ayrx
Ayrx

Reputation: 2162

Zend Framework: How exactly does view work?

I have this function in my PurchaseController.

    public function viewAction()
    {
    $detail = new Application_Model_Dbtable_Purchasedetails();
    $purchaseid = $this->getRequest()->getParam('purchaseid');
    $select = $detail->select()
    ->from(array('c' => 'purchasedetails'))
    ->join(array('p' => 'product'), 'p.productid = c.productid')
    ->where('purchaseid = ?', $purchaseid)
    ->setIntegrityCheck(false);
    $fetch = $detail->fetchAll($select);
    $this->view->purchase = $fetch;
    }

I have this code in my view.phtml

foreach($this->view as $fetch) :?>
<tr>
<td><?php echo $this->escape($detail['productid']);?></td>
<td><?php echo $this->escape($detail['name']);?></td>
<td><?php echo $this->escape($detail['quantity']);?></td>
<td><?php echo $this->escape($detail['price']);?></td>
<td><?php echo $this->escape($detail['price']*$detail['quantity']);?> </td>
</tr>

However, i get this error message.

Warning: Invalid argument supplied for foreach() in

What is the cause of and solution to this error? Many thanks.

Upvotes: 0

Views: 331

Answers (1)

StoryTeller
StoryTeller

Reputation: 1748

The argument in your foreach has to be $this->purchase instead of $this->view.

foreach($this->purchase as $fetch) {
    // Your code here
}

The class variable $this->view is used by Zend_Controller_Action to load any variables into your view, its values will be given, not the variable itself. Thats why $this->view is not set.

So simply leave out ->view inside of the view, e.g. $this->view->variableName always becomes $this->variableName into your view script.

Upvotes: 2

Related Questions