从数组中检索数据

时间:2011-12-08 23:54:48

标签: cakephp

我有这个数据从我的蛋糕php查询吐出..这是问题..我如何检索这些数据,使其适合这种结构。我怎么能在我看来这样做呢?我只是在理解如何检索每个值

所以这部分我希望它在我的页面中的<p></p>

array
  'id' => string '1' (length=1)
  'category_name' => string 'Appetizers' (length=10)
  'category_keywords' => string 'appetizer, appetizers' (length=21)
  'category_title' => string 'Our Side Dishes' (length=15)
  'category_info' => string 'Hello This is the category info' (length=30)
  'dish_id' => string '1' (length=1)

这个查询的其他部分返回我想把它放在一个3 x 2的表中。像

<table>
<tr>
 <td> the data from the array 0</td>
 <td> the data from the array 1</td>
 <td> the data from the array 2>/td>
</tr>
<tr>
 <td> the data from the array 3</td>
 <td> the data from the array 4</td>
 <td> the data from the array 5>/td>
</tr>

这里是所有那些数组。我想使用带有mod的for循环,如果3%== 0,然后创建一个新的<tr>,我试了但是它给了我一个错误......

 array
  'id' => string '1' (length=1)
  'category_name' => string 'Appetizers' (length=10)
  'category_keywords' => string 'appetizer, appetizers' (length=21)
  'category_title' => string 'Our Side Dishes' (length=15)
  'category_info' => string 'Hello This is the category info' (length=441)
  'dish_id' => string '1' (length=1)

array
  0 => 
    array
      'id' => string '1' (length=1)
      'dish_name' => string 'Arepa Con Pollo!' (length=16)
      'dish_disc' => string 'Exquisite corn patti, the best yyy' (length=34)
      'dish_price' => string '2.90' (length=4)
      'dish_image' => string '/img/arepa rellena con pollo.jpeg' (length=33)
      'dish_category_id' => string '1' (length=1)
      'dish_price_label' => string 'Delicious Arepa ' (length=16)
  1 => 
    array
      'id' => string '2' (length=1)
      'dish_name' => string 'Arepa Con Queso' (length=15)
      'dish_disc' => string 'Corn patie with Cheese' (length=22)
      'dish_price' => string '2.65' (length=4)
      'dish_image' => string '/img/arepa rellena con queso.jpeg' (length=33)
      'dish_category_id' => string '1' (length=1)
      'dish_price_label' => string '' (length=0)
  2 => 
    array
      'id' => string '3' (length=1)
      'dish_name' => string 'Empanadas de Carne' (length=18)
      'dish_disc' => string 'Patties with beef' (length=17)
      'dish_price' => string '1.60' (length=4)
      'dish_image' => string '/img/empanadas de carne.jpeg' (length=28)
      'dish_category_id' => string '1' (length=1)
      'dish_price_label' => string '' (length=0)
  3 => 
    array
      'id' => string '4' (length=1)
      'dish_name' => string 'Empanadas de Pollo' (length=18)
      'dish_disc' => string 'Patties with Chicken' (length=20)
      'dish_price' => string '1.94' (length=4)
      'dish_image' => string '/img/empanadas de pollo.jpeg' (length=28)
      'dish_category_id' => string '1' (length=1)

除了这些之外还有更多数组..但它们都具有相同的格式。

这些来自使用以下控制器

class DishCategory extends AppModel{

           public $hasMany = array(
            'Dish' => array(
                'className' => 'Dish',
                'foreignKey' => 'dish_category_id'
            )
        );

 }

class DishCategoriesController extends AppController {

 function get_categories($id)
      { 
        // find category with a dish of $id
        $this->set('dishes', $this->DishCategory->find('first', array(
          'conditions' => array(
            'DishCategory.id' => $id
          )
        )));

        // set master layout
        $this->layout = 'master_layout';
      }     
  }

1 个答案:

答案 0 :(得分:0)

使用this->set('variableName', $data);将数组从控制器传递到视图。

然后,您可以在视图中以$variableName的形式访问上述示例。我建议先做一个<?php debug($variableName); ?>来查看你的数组在视图中的样子。它应该是相同的,但它将帮助您了解要使用的索引。

了解更多信息:http://book.cakephp.org/view/979/set