當試圖運行此代碼時,Cake在我們的foreach循環中拋出一個錯誤,我將包含模型,控制器和視圖。此代碼的最終目標是打印出用戶自己的發票,而不是數據庫中的所有發票。cakephp foreach error
模型
<?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>
你嘗試過像...在'foreach'使用不同的變量名稱爲'as'? –
它會拋出一個意見,說foreach行意外的T_String – user1393064
'foreach($ invoice as $ invoice):'??? XD加油!你知道這個問題有什麼問題= P – pleasedontbelong