Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm having some problems getting this to work, I am sure there's an easy explanation, but I am not able to connect the dots at the moment.

I'm using Yii Framework 1.1.13 with the Twitter Bootstrap.

My controller looks like this:

public function actionIndex()
{
    $posts = array(
        'total' => 5,
        'items' => array(
            array(      
                'id' => 1,
                'header' => 'Praesent arcu nisi',
                'body' => 'Lacinia a dolor at, tempus suscipit diam.Lacinia a dolor at, tempus suscipit diam.Lacinia a dolor at, tempus suscipit diam.Lacinia a dolor at, tempus suscipit diam.Lacinia a dolor at, tempus suscipit diam.',
                'img' => '',
                'link' => 'http://www.example.com',
                'category' => 'Test',
                'company' => 'Example',
                'company_bkg' => '',
                'rating' => 8,
                'comments_total' => 2,
                'comments_items' => array(
                    array(
                        'id' => 1,
                        'name' => 'Anonymous',
                        'timestamp' => date('d.m.Y H:i'),
                        'text' => 'Kommentar 1.',
                        'rating' => 145,
                    ),
                    array(
                        'id' => 2,
                        'name' => 'Example',
                        'timestamp' => date('d.m.Y H:i'),
                        'text' => 'Kommentar 2.',
                        'rating' => -29,
                    ),
                ),
            ),
            array(      
                'id' => 2,
                'header' => 'Lacinia a dolor at',
                'body' => 'Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi.',
                'img' => '',
                'link' => 'http://www.example.com',
                'category' => 'Test',
                'company' => 'Example',
                'company_bkg' => '',
                'rating' => 0,
                'comments_total' => 0,
                'comments_items' => array(
                    array(

                    ),
                ),
            ),
            array(      
                'id' => 3,
                'header' => 'Lacinia a dolor at',
                'body' => 'Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi.',
                'img' => '',
                'link' => 'http://www.example.com',
                'category' => 'Test',
                'company' => 'Example',
                'company_bkg' => '',
                'rating' => -8,
                'comments_total' => 1,
                'comments_items' => array(
                    array(
                        'id' => 1,
                        'name' => 'Anonymous',
                        'timestamp' => date('d.m.Y H:i'),
                        'text' => 'Kommentar 1.',
                        'rating' => 9,
                    ),
                ),
            ),
        ),
    );

    $this->render('index',array('posts'=>$posts));
}

And the view looks like this:

/* @var $this SiteController */
/* @var $posts array */
/* @var $comment array */

...
foreach($posts['items'] as $post) {
...
    for($i = 0; $i < $length; $i++) { 
        $comment = $post['comments_items'][$i];
        var_dump($comment);

...

Which shows the following var_dump:

array (size=5)
  'id' => int 1
  'name' => string 'Anonymous' (length=9)
  'timestamp' => string '29.06.2013 15:57' (length=16)
  'text' => string 'Kommentar 1.' (length=12)
  'rating' => int 145

But when I do a $comment['rating'], it comes back with an error:

Undefined index: rating

Anyone have any idea what I'm doing wrong?

share|improve this question
up vote 1 down vote accepted

You specify the second array like this:

        array(      
            'id' => 2,
            'header' => 'Lacinia a dolor at',
            'body' => 'Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi.',
            'img' => '',
            'link' => 'http://www.example.com',
            'category' => 'Test',
            'company' => 'Example',
            'company_bkg' => '',
            'rating' => 0,
            'comments_total' => 0,
            'comments_items' => array(
                array(

                ),
            ),
        ),

There is no rating entry in the comments_items, that's why you getting the error.

Try adding the rating element too:

        array(      
            'id' => 2,
            'header' => 'Lacinia a dolor at',
            'body' => 'Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi. Praesent arcu nisi.',
            'img' => '',
            'link' => 'http://www.example.com',
            'category' => 'Test',
            'company' => 'Example',
            'company_bkg' => '',
            'rating' => 0,
            'comments_total' => 0,
            'comments_items' => array(
                array(
                    'rating' => 1
                ),
            ),
        ),
share|improve this answer

The name of your variable is $posts, not $post.

Look the name in this line:

$this->render('index',array('posts'=>$posts));
share|improve this answer
    
Thanks for trying, but this was because I forgot to add a line of code to the view shown here. foreach($posts['items'] as $post) { has been edited into the code sample above. – rnngau Jun 29 '13 at 16:27
    
Is it suposed that $length variable within for stament is comments_total key? – user1648170 Jun 29 '13 at 17:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.