0

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?

2 Answers 2

2

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

Look the name in this line:

$this->render('index',array('posts'=>$posts));
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Is it suposed that $length variable within for stament is comments_total key?
1

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
                ),
            ),
        ),

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.