1

では、どうすればこれを修正できますか?それは私に与えます:

注意(8):未定義の変数:製品[APP / View / Category / category_filter.ctp、10行目]警告(2):foreach()に無効な引数が指定されました[APP / View / Categorys / 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));
    }   
4

2 に答える 2

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。製品モデルでも、そのモデルの関連付けを行ってください。

于 2012-08-08T11:46:03.800 に答える
0
<?php
function category_filter($category_id) {
    $this->set('categories',$this->Category->findAllById($category_id));
    $products=$this->Product->find('all');
   $this->set(compact('products'));
    }   
?>

商品変数を設定する

于 2012-08-08T11:40:54.747 に答える