Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 957431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:41:56+00:00 2026-05-16T00:41:56+00:00

I’m trying to get simplepie to loop through a few rss feeds using codeigniter

  • 0

I’m trying to get simplepie to loop through a few rss feeds using codeigniter but I can only get it to display the feed items from the last feed in the array.

I think it is to do with the foreach loop in the _get_feed function but everything I’ve tried has failed to fix it.

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 view I have this

<?php foreach($feed as $item) : ?>
    <br />
    <a href="<?php echo $item['permalink']; ?>"><?php echo $item['title']; ?  ></a>
<?php endforeach; ?>

I’m aware that it’s possible to give simplepie an array of feeds to parse with the set_feed_url function but I don’t want to do this as it mixes all the feed items together.

I’d also like to know if placing the _get_feed function in the controller is ok in terms of best practises or would it be better off in a model as it is fetching data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:41:57+00:00Added an answer on May 16, 2026 at 12:41 am

    As far as I am aware your $data array was not global and therefore only had scope within the index function.

    As far as conventions go, I only use models for database connections, but I don’t think there are any hard and fast rules.

    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['feed'][] = $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)
        {
            $return[$count]['title'] = $item->get_title();
            $return[$count]['permalink'] = $item->get_permalink();
            $count ++;
        }       
        return $return;
    }
    
    }
    

    This will give you:

    $data['feed'][0] = details from 1st url
    $data['feed'][1] = details from 2nd url
    

    So to access the title from the 1st article in the 1st feed:

    $data['feed'][0][0]['title']
    //or within the view
    $feed[0][0]['title'];
    

    If you wanted to merge the URL’s together you would do something like:

    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)
        {
            $feed_info[] = $this->_get_feed($feed_url);
            $data['feed'][] = $this->_get_feed($feed_url);
        }
    
        foreach ($feed_info as $feed)
        {
            foreach($feed as $feed_item)
            {
                $data['feed'][] = array( 'title' => $feed_item->get_title(), 'permalink' => $feed_item->get_permalink() );
            }
        }
    
        $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();
    
        return $feed;
    }
    
    }
    

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.