Welcome Guest, Not a member yet? Register   Sign In
Creating a simple news aggregator..help with keeping it MVC please.
#1

[eluser]jk215[/eluser]
Basically Im using SimplePie as a library inside of CI. I have everything working and displaying 1 feed from one source. My question is how will I lay this out, using MVC, to display multiple seperate feeds from different sources on one page? Exactly like http://popurls.com does. I am not currently using a database/model but I assume I will be storing the site name and feed url in one once I get going. Im just having trouble visualizing how this would work for a lot of feeds without repeating myself a bunch.

My code for displaying 1 feed is (controller):
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Feeds extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $siteurl = "http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml";
        $data['feed'] = new SimplePie();
        $data['feed']->set_feed_url($siteurl);
        $data['feed']->enable_cache(false);
        $data['feed']->init();
        $data['feed']->handle_content_type();
        $this->load->view('feeds/index', $data);
    }
}

View:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

&lt;html &gt;
&lt;head&gt;
    &lt;title&gt;Sample SimplePie Page&lt;/title&gt;
    &lt;meta http-equiv="content-type" content="text/html; charset=UTF-8" /&gt;
&lt;/head&gt;
&lt;body&gt;
<ul>
        &lt;?php if ($feed->data): ?&gt;
    &lt;?php $items = $feed->get_items(0, 5); ?&gt;
        &lt;?php  foreach ($items as $item): ?&gt;
            <li><strong><a >get_permalink(); ?&gt;">&lt;?php echo $item->get_title(); ?&gt;</a></strong> - &lt;?php echo $item->get_date('j M Y'); ?&gt;</li>
        &lt;?php endforeach; ?&gt;
&lt;?php endif; ?&gt;
</ul>
#2

[eluser]n0xie[/eluser]
Untested general example:
Code:
// controller
    function index()
    {
        // you might want to put this in a db
        $feeds = array('reddit' => 'http://kb03.com/reddit500.rss',
                        'digg' => 'http://services.digg.com/2.0/story.getTopNews?type=rss&topic=technology',
                        'hackernews' => 'http://news.ycombinator.com/rss',
                        'bbc' => 'http://feeds.bbci.co.uk/news/world/us_and_canada/rss.xml'
                        );
        foreach ($feeds as $site=>$url)
        {
            $data['feed'][$site] = new SimplePie();
            $data['feed'][$site]->set_feed_url($url);
            $data['feed'][$site]->enable_cache(false);
            $data['feed'][$site]->init();
            $data['feed'][$site]->handle_content_type();
        }
        $this->load->view('feeds/index', $data);
    }
    
    // view
    
    &lt;?php foreach ($feed as $site): ?&gt;

         <ul>

         &lt;?php if ($site->data): ?&gt;
        
            &lt;?php $items = $site->get_items(0, 5); ?&gt;
            &lt;?php foreach ($items as $item): ?&gt;
            <li><strong><a href="">&lt;?php echo $item->get_title(); ?&gt;</a></strong> - &lt;?php echo $item->get_date('j M Y'); ?&gt;</li>
            &lt;?php endforeach; ?&gt;
            
        &lt;?php endif; ?&gt;
        
        </ul>
        
    &lt;?php endforeach;
#3

[eluser]jk215[/eluser]
Thanks a lot I tested it out and its got me going in the right direction!




Theme © iAndrew 2016 - Forum software by © MyBB