当试图运行这个代码蛋糕时,我们的 foreach 循环在视图中抛出了一个错误,我将包括模型、控制器和视图。此代码的最终目标是打印出用户自己的发票,而不是数据库中的每张发票。
模型
<?php
class Invoice extends AppModel{
var $name='invoice';
public $useTable = 'invoices';
public $primaryKey = 'id';}
具有相关功能的控制器
<?php
class InvoicesController extends AppController{
public function index(){
$this->set('title_for_layout', 'indexPage');
}
public function payinvoice($id = null){
$this->set('title_for_layout', 'Pay Invoice');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.jpg');
$this->layout='home_layout';
$this->set('invoice', $this->Invoice->read(null, $id));
}
public function viewinvoice(){
$this->set('title_for_layout', 'View invoice');
$this->set('stylesheet_used', 'homestyle');
$this->set('image_used', 'eBOXLogoHome.jpg');
}
查看文件
<table width="100%" border="1">
<table width="100%" border="1">
<tr>
<th>Biller</th>
<th>Subject</th>
<th>Date</th>
<th>Action</th>
</tr>
<?php foreach($invoice as $invoice): ?>
<tr><td align='center'>
<?php echo $this->Html->link($invoice['Invoice']['biller'],
array('action' => 'viewinvoice', $invoice['Invoice']['id'])); ;?> </td>
<td align='center'><?php echo $invoice['Invoice']['subject']; ?></td>
<td align='center'><?php echo $invoice['Invoice']['datecreated']; ?></td>
<td align='center'><a href="viewinvoice"><button>View Invoice</button></a><a href="disputeinvoice"><button>Dispute Invoice</button></a></td>
</tr><?php endforeach; ?>
</table>