cakephp notice 8:undefined index:

时间:2015-04-24 18:31:22

标签: cakephp

我在我的view.ctp页面上获取了类别,产品和尺寸的cakephp通知8。

以下是我的ProductsController视图:

public function view($id) {
    $result = $this->Product->find('all', array(
        'contain' => array(
            'Category' => array(
                'Size'
            )

        ),
        'conditions' => array(
            'Product.id' => $id
        )

    ));

     $this->set(compact(
        'result'
    ));

这是view.ctp:

<?php

$product = $result['Product'];
$category = $result['Category'];
$sizes = $category['Size'];

pr($result);

echo "<h2>" . $product['name'] . "</h2>";
echo "<div>". $product['description'] . "</div>";

echo $this->Form->create();
echo $this->Form->input('sizes');


echo "<h3>" . $size['name'] . "</h3>";

//echo $this->Html->link('Add to Paypal cart', $paypalUrl);

echo '<br />';
echo '<br />';

echo '<h3>Add to cart</h3>';

echo $this->Form->create(false); //  FALSE FOR NO MODEL
echo $this->Form->input('quantity');
echo $this->Form->submit('Add to cart');
echo $this->Form->end();


?>

我做了pr($result);,数组显示了我需要的所有信息:

Array
(
    [0] => Array
        (
            [Product] => Array
                (
                    [id] => 115
                    [name] => Curly Indian Fusions Extensions
                    [description] => Small groups of luxurious 100% virgin human hair strands are matched to coloured high-quality keratin tips
                    [image] => ../webroot/img/hciImages/curlyVirginUtipSMA.jpg
                    [category_id] => 10
                    [created] => 0000-00-00
                    [modified] => 0000-00-00
                )

            [Category] => Array
                (
                    [id] => 10
                    [name] => Indian Fusion Extensions
                    [desc] => Small groups of luxurious 100% virgin human hair strands are attached to matched colour high quality keratin tips.
                    [Size] => Array
                        (
                            [0] => Array
                                (
                                    [id] => 225
                                    [name] => 12 inches
                                    [category_id] => 10
                                    [price] => 2.50
                                )

                            [1] => Array
                                (
                                    [id] => 226
                                    [name] => 14 inches
                                    [category_id] => 10
                                    [price] => 2.50
                                )

                            [2] => Array
                                (
                                    [id] => 227
                                    [name] => 16 inches
                                    [category_id] => 10
                                    [price] => 2.50
                                )

                            [3] => Array
                                (
                                    [id] => 228
                                    [name] => 18 inches
                                    [category_id] => 10
                                    [price] => 3.00
                                )

                            [4] => Array
                                (
                                    [id] => 229
                                    [name] => 20 inches
                                    [category_id] => 10
                                    [price] => 3.50
                                )

                            [5] => Array
                                (
                                    [id] => 230
                                    [name] => 22 inches
                                    [category_id] => 10
                                    [price] => 3.50
                                )

                            [6] => Array
                                (
                                    [id] => 231
                                    [name] => 24 inches
                                    [category_id] => 10
                                    [price] => 4.50
                                )

                            [7] => Array
                                (
                                    [id] => 232
                                    [name] => 26 inches
                                    [category_id] => 10
                                    [price] => 4.60
                                )

                        )

                )

        )

)

所有信息都在那里,但没有显示。我有一种感觉,我没有正确地调用信息,但无法确定为什么信息没有定义,如果有的话。任何指导都很感激,我是一个菜鸟。

谢谢

1 个答案:

答案 0 :(得分:0)

您应该使用loop来表示$result的数据。在这种情况下只需替换

$product = $result[0]['Product'];
$category = $result[0]['Category'];
$sizes = $category['Size'];

代替

$product = $result['Product'];
$category = $result['Category'];
$sizes = $category['Size']

更新

foreach($sizes as $size)  {
   echo "<h3>" . $size['name'] . "</h3>";
}