1

I'm using cakephp 2.2.5 for this.

I've got a contained find in one controller that pulls a list of four news items from a 'has many' relationship.

I'm trying to produce a list of these four items, but can't seem to get the php foreach loop working.

the controller array is this:

    $newsLists = $this->Industry->find('all', array(
        'conditions' => array('id' => $id),
        'fields' => array('id'),
        'contain' => array(     
            'News' => array(
                'conditions' => array('News.type' => 'main','News.active' => 'Yes'),                        
                    'fields' => array(
                            'News.type', 
                            'News.id',
                            ),
                    'order' => array(
                        'News.created' => 'desc',
                       ),
                    'limit' => 3
        ))));


    $this->set('newsLists', $newsLists);

The debug output works fine:

  Array
(
[0] => Array
    (
        [Industry] => Array
            (
                [id] => 1
                [tags] => 
            )

        [News] => Array
            (
                [0] => Array
                    (
                        [type] => main
                        [id] => 10
                        [industry_id] => 1
                    )

                [1] => Array
                    (
                        [type] => main
                        [id] => 11
                        [industry_id] => 1
                    )

                [2] => Array
                    (
                        [type] => main
                        [id] => 12
                        [industry_id] => 1
                    )

            )

    )

)

but this foreach loop only display one item:

<ul>
<?php 
$i = 0;

foreach ($newsLists as $newsList): ?>

        <li><?php echo $newsList['News'][$i]['slug']; ?></li>

<?php endforeach; ?>

thanks

3 Answers 3

2

It should be -

<?php foreach ($newsLists['News'] as $newsList): ?>

        <li><?php echo $newsList['field to print']; ?></li>

<?php endforeach; ?>

Some points you should take care off -

There is no slug field.

You have mentioned fields - type & id. But the industry_id is also fetched.
Sign up to request clarification or add additional context in comments.

Comments

1

You have there two problems:

  1. you echoes an entry using $i but never increment this value, $i is still 0. And there is only one-step loop, you loop over the array with only one record.

  2. slug isn't defied in your array, only one of type, id, industry_id can be used

Ad 1.
Correct way is:

foreach ($newsLists['News'] as $newsList) {
    echo '<li>' . $newsList['type'] . '</li>'; // you can switch type to id or industry_id
}

4 Comments

thanks for the two tips. I've now made this change but still receive an error: <ul> <?php for ($i = 0; $i <= 3; $i++) foreach ($newsLists['News'] as $newsList): ?> <li><?php echo $newsList['News'][$i]['slug']; ?></li> <?php endforeach; ?> </ul> Errors: Notice (8): Undefined index: News, Warning (2): Invalid argument supplied for foreach(). I've also added slug into the field list.
@Paul: you need no for loop there. What I answered is full code, just these three lines. No $i is needed.
thanks @panther. I've added the following, but am still getting the 'Notice (8): Undefined index: News' and 'Warning (2): Invalid argument supplied for foreach()', code I changed to: <?php foreach ($newsLists['News'] as $newsList) { echo '<li>' . $newsList['type'] . '</li>'; } ?>
hi @panther, I've resolved this now. I suspected that since this there is a has many relationship between industry and news that I would need another loop. This could works now: <?php foreach ($newsLists as $newsList): ?> <ul> <?php foreach ($newsList ['News'] as $list): ?> <li><?php echo $list['slug']; ?></li> <?php endforeach; ?> </ul> <?php endforeach; ?>
0

The resolution involves creating another loop since there is a has many relationship between industry and news. This extra loop allows the news stories to be looped through.

<?php foreach ($newsLists as $newsList): ?>
 <ul>
    <?php foreach ($newsList ['News'] as $list): ?>
    <li><?php echo $list['slug']; ?></li>
   <?php endforeach; ?>
 </ul>
<?php endforeach; ?>

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.