-2

Im looking to echo the value of my array key out into the view for this controller. I cant quite figure it out. So for the first feed I want to echo mmafighting into the of the view. Then it would loop through the rest.

Here is my controller code:

function index()
{
$this->load->library('simplepie');
       $feeds = array('mmafighting' => 'http://www.mmafighting.com/rss/current',
                    'bbc' => 'http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml',
                    'bloodyelbow' => 'http://www.bloodyelbow.com/rss',
                    'ufc' => 'http://www.ufc.com/rss/news',
                    'hackernews' => 'http://news.ycombinator.com/rss',
                    'msnbc' => 'http://www.msn.com/rss/news.aspx',
                    'msnbc2' => 'http://www.msn.com/rss/msnmoney.aspx',
                    'msnbc3' => 'http://www.msn.com/rss/msnshopping_top.aspx'

                 );
    foreach ($feeds as $site=>$url)
    {
        $data['feed'][$site] = new SimplePie();
        $data['feed'][$site]->set_feed_url($url);
        $data['feed'][$site]->set_cache_location(APPPATH.'cache');
        $data['feed'][$site]->set_cache_duration(300);
        $data['feed'][$site]->init();
        $data['feed'][$site]->handle_content_type();
    }
    $this->load->view('feedsview', $data);
}

Here is my view code:

<?php
$feedCount = 0; 
$rowCount = 0;
?>
<?php foreach ($feed as $site): ?>

    <?php if ($site->data): ?>
            <div class="feed">
            <h2><?php // echo original array key here ?></h2>
            <ul>
            <?php $items = $site->get_items(0, 5); ?>
            <?php foreach ($items as $item): ?>
                <li><a href="<?php echo $item->get_link(); ?>"><?php echo $item->get_title(); ?></a></li>
            <?php endforeach; ?>
        </ul>
        </div>
        <?php endif; ?>



<?php $feedCount++; ?>
<?php if ($feedCount === 3) {
    echo '<div style="clear:both;">&nbsp;</div>';
    $feedCount = 0;
    $rowCount++;
        if ($rowCount === 2) {
            echo '<div style="width:1000px; margin-bottom:20px;height:100px; border:1px solid #252525; float:left;">images</div>';
            }
    } ?>
    <?php endforeach; ?>
jkphl
  • 181
  • 8
  • 21
  • 4
    try `array_keys` http://php.net/manual/en/function.array-keys.php, or `foreach($key => $value)` http://php.net/manual/en/control-structures.foreach.php – Marek Sebera Apr 27 '12 at 20:23

1 Answers1

1

I'm not sure i understand but what about doing this :

<?php foreach ($feed as $key => $site): ?>

    <?php if ($site->data): ?>
            <div class="feed">
            <h2><?php echo $key ?></h2>
            <ul>
Gaet
  • 699
  • 3
  • 11
  • If I try to echo $key it says undefined variable. If I echo key($site) it just says "data" for every one. – jkphl Apr 27 '12 at 20:41
  • 1
    key returns the key of the element on which the array pointer currently is so in your case, you should probably do key($feed). could you try to print_r the complete $feed so you can see what it looks like? – Gaet Apr 27 '12 at 20:48
  • If I do key($feed) it returns 'bbc' for every feed set. Looks like thats on the right track. – jkphl Apr 27 '12 at 20:52
  • And what did the print_r say? – Gaet Apr 27 '12 at 21:12
  • A whole bunch of stuff like this: Array ( [mmafighting] => SimplePie Object ( [data] => Array ( [child] => Array ( [http://www.w3.org/2005/Atom] => Array ( [feed] => Array ( [0] => Array ( [data] => [attribs] => Array ( [http://www.w3.org/XML/1998/namespace] => Array ( [lang] => en ) ) [xml_base] => [xml_base_explicit] => [xml_lang] => en [child] => Array ( [http://www.w3.org/2005/Atom] => Array ( [title] => – jkphl Apr 27 '12 at 21:15
  • Then it should be ok... Are you sure you don't pass other information to the view besides 'feed' in $data? – Gaet Apr 27 '12 at 21:18
  • Thats it. That controller code is all I got so far. I could move the

    inside of the item loop and use get_title() but I want to label the feeds with my own name.

    – jkphl Apr 27 '12 at 21:22
  • 1
    Then for the sake of debugging, could you try to loop on $data in your controller and echo $key? just to see if the result is ok? If it is, you could try to remove everything from your view save for the same loop that would only display $key also? – Gaet Apr 27 '12 at 21:31
  • I managed to get this fixed by adding a set and get title function to the SimplePie class. Thanks for your help though it got me going in the right direction! – jkphl Apr 30 '12 at 14:49