cakephp中未定义的变量

cakephp中未定义的变量,cakephp,Cakephp,那么我该如何解决这个问题呢?它给了我: <?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 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; ?>
注意(8):未定义变量:products [APP/View/Categories/category_filter.ctp,第10行]警告(2): 为foreach()提供的参数无效 [APP/View/Categories/category_filter.ctp,第10行]

 <?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; ?>

设置产品变量


 <?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; ?>

设置产品变量

只需创建一个
类别模型类
即可

//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';
}
 <?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; ?>
在控制器中:

function category_filter($category_id) {
    $this->set('category_details',$this->Category->findById($category_id));
}  
 <?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; ?>
在你看来:

<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; ?>
类别:
名称描述价格类别
缩略图

 <?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; ?>

 <?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; ?>

 <?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; ?>

因为我不太了解您的
图片模型
。请在您的产品模型中也对该模型进行关联。

您只需创建一个
类别模型类即可

//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';
}
 <?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; ?>
在控制器中:

function category_filter($category_id) {
    $this->set('category_details',$this->Category->findById($category_id));
}  
 <?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; ?>
在你看来:

<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; ?>
类别:
名称描述价格类别
缩略图

 <?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; ?>

 <?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; ?>

 <?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; ?>

因为我不太了解您的
图片模型
。请在您的产品模型中也对该模型进行关联。

您没有在控制器的方法中设置产品变量。您能告诉我正确的方法吗?我试过了,但出现了一个致命错误。您的表结构、产品和类别模型是什么?请在此解释,以便我能给您提供更好的解决方案。带有| id | category |产品的类别是| id | name | description | category | u id |您没有在控制器的方法中设置products变量。您能告诉我正确的方法吗?我试过了,但出现了一个致命错误。您的表结构、产品和类别模型是什么?请在此解释,以便我能给您提供更好的解决方案。带有| id | category |产品的类别是| id | name | description | category | u id |图片模型:var$belongsTo=array('Product');产品模式:var$hasMany=array('Picture');图片表具有product_id foreign keyPicture模型:var$belongsTo=array('product');产品模式:var$hasMany=array('Picture');图片表具有product_id外键
 <?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; ?>