I'm trying to get simplepie to loop on multiple rss feeds using codeigniter, but I can only get it to display feed items from the last feed in the array.
I think this is due to the foreach loop in the _get_feed function, but everything I tried failed to fix.
In my controller, I have this
class Main extends Controller{
function index()
{
$this->load->library( 'Simplepie' );
$feed_urls = array
(
'http://feeds.feedburner.com/SputnikmusicNews',
'http://feeds2.feedburner.com/nmecom/rss/newsxml',
);
foreach ($feed_urls as $feed_url)
{
$data = $this->_get_feed($feed_url);
}
$data['main_content'] = 'main_view';
$this->load->view('includes/template', $data);
}
function _get_feed($url)
{
$feed = new SimplePie();
$feed->set_feed_url($url);
$feed->init();
$count = 0;
foreach($feed->get_items() as $item)
{
$data['feed'][$count]['title'] = $item->get_title();
$data['feed'][$count]['permalink'] = $item->get_permalink();
$count ++;
}
return $data;
}
}
Then, in my opinion, I have it
<?php foreach($feed as $item) : ?>
<br />
<a href="<?php echo $item['permalink']; ?>"><?php echo $item['title']; ? ></a>
<?php endforeach; ?>
I know that you can give simplepie an array of parses for parsing using the set_feed_url function, but I don’t want to do this because it mixes all the elements of the feed together.
I would also like to know if the _get_feed function in the controller supports in terms of best practices or would it be better in the model as it retrieves the data?