0

我的 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;
    }

我的 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>

但是,我收到此错误消息。

Warning: Invalid argument supplied for foreach() in

这个错误的原因和解决方法是什么?非常感谢。

4

1 回答 1

2

foreach 中的参数必须是 $this->purchase 而不是 $this->view。

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

Zend_Controller_Action 使用类变量 $this->view 将任何变量加载到您的视图中,它的值将被给出,而不是变量本身。这就是为什么没有设置 $this->view 的原因。

因此,只需在视图中省略 ->view,例如 $this->view->variableName 总是在您的视图脚本中变为 $this->variableName。

于 2012-08-09T07:38:08.710 回答