Using multiple channels in simplepie with codeigniter

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?

+3
1

, $data .

, , , .

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;
}

}

:

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

, 1- 1- :

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

URL , - :

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;
}

}
+3

Source: https://habr.com/ru/post/1756468/


All Articles