2012-08-08 126 views
1

那麼我該如何解決這個問題?這使我:未定義變量在cakephp

通知(8):未定義變量:產品 [APP /視圖/分類/ category_filter.ctp,線10]警告(2): )提供的foreach(無效參數 [APP /View/Categories/category_filter.ctp,線10]

<? if (!empty($categories)) { ?> 

<? foreach($categories as $row): ?> 
<h1>Category: <?= $row['Category']['category'] ?></h1> 

<table> 
    <tr> 
     <th>Name</th><th>Description</th><th>Price</th><th>Category</th> 
<th>Thumbnails</th> 
    </tr> 
    <? foreach($products as $row): ?> 
    <tr><<td> 
     <?=$row['Product']['name']?> 
    </td><td> 
     <?=$row['Product']['description']?> 
    </td><td> 
     <?=$row['Product']['price']?> 
    </td><td> 
     <?=$row['Product']['category_id']?> 
    </td><td> 
    <? 
    if(!empty($row['Picture'])){ 
    ?> 
    <?= $this->Html->image('/img/'. $row['Picture'][0]['filename'], array('width'=>'50', 'alt'=>$row['Picture'][0]['title'])); ?> 
      <p><?= $row['Picture'][0]['description']; ?></p> 
    <? } ?>    
    </td><tr> 

    <? endforeach; ?> 
    <? endforeach; ?> 


</table> 

<? } ?> 

在控制器

function category_filter($category_id) { 
     $this->set('categories',$this->Category->findAllById($category_id)); 
    } 
+2

您沒有在您的控制器的方法中設置產品變量。 – 2012-08-08 11:05:02

+0

你能告訴我正確的做法嗎?我嘗試過,但得到了一個致命的錯誤。 – user1461577 2012-08-08 11:19:03

+0

什麼是您的表格結構和產品和類別模型?請在這裏解釋,以便我可以給你一個更好的解決方案。 – 2012-08-08 11:23:52

回答

1

您可以簡單地通過製作Category Model class來做到這一點。

//app/Model/Category.php 
class Category extends AppModel 
{ 
    public $name = 'Category'; 
    public $useTable = 'categories';  
    public $primaryKey = 'id'; 
    public $hasMany = array('Products' => array('className' => 'Product', 
              'foreignKey' => 'category_id' 
              ));  
} 

//app/Model/Product.php 
class Product extends AppModel 
{ 
    public $name = 'Product'; 
    public $useTable = 'products';  
    public $primaryKey = 'id'; 
} 

在你的控制器:

function category_filter($category_id) { 
    $this->set('category_details',$this->Category->findById($category_id)); 
} 

在你看來:

<h1>Category: <?php echo $category_details['Category']['category'] ?></h1> 

<table> 
<tr> 
    <th>Name</th><th>Description</th><th>Price</th><th>Category</th> 
<th>Thumbnails</th> 
</tr> 
<?php if(!empty($category_details['Products'])): foreach($category_details['Products'] as $row): ?> 
<tr><td> 
    <?php echo $row['Product']['name']?> 


<?php echo $this->Html->image('/img/'. $row['Picture'][0]['filename'], array('width'=>'50', 'alt'=>$row['Picture'][0]['title'])); ?> 
     <p><?php echo $row['Picture'][0]['description']; ?></p> 
<?php } ?>    
</td><tr> 
<?php endforeach;endif; ?> 

,因爲我不知道很多關於你的Picture Model。請在您的產品模型中進行該模型的關聯。

+0

圖片模型:var $ belongsTo = array('Product');產品模式:var $ hasMany = array('Picture'); – user1461577 2012-08-08 12:36:08

+0

圖片表具有product_id外鍵 – user1461577 2012-08-08 12:36:39

0
<?php 
function category_filter($category_id) { 
    $this->set('categories',$this->Category->findAllById($category_id)); 
    $products=$this->Product->find('all'); 
    $this->set(compact('products')); 
    } 
?> 

設置您的產品變量