Welcome Guest, Not a member yet? Register   Sign In
RSS aggregator
#1

[eluser]Kurai[/eluser]
Hi everyone.
I'm really starting with CI, I like it al lot, but, not being a much of a coder, something is still difficult to figure out for me.

I'm trying to do an RSS aggregator, which displays several sources in several ways (I.E: the last post from a blog, flickr images, the most n recent posts for another blog and so on).

Right now I'm proceeding like this: I've made an aggregator view which act as a container, and several other views for various bits of codes (header, footer, and the various RSS boxes) to easily edit them.

Then I've installed Simplepie as a class and tried fetching a single RSS source, following literally the tutorial at http://www.nextbigleap.com/blog/developm...deigniter/.

This worked pretty fine, but I'm stuck now. I know I should create a function which takes an url as an argument and then returns the array with the rss feed, but I can't figure out where to create it (a new library maybe?) and how to call it correctly from the view in which I want to display my feed.

I know thisi is probably quite stupid as a question, but I still have difficulties in understanding the differences between classes, models and libraries.

Can anyone lend me a hand? Thank you!
#2

[eluser]NemetraL[/eluser]
Following the MVC principles, it's not the view that will call your library, it's the controller.
The controller calls the library, puts the data in a variable and calls the view with this variable as an argument.

For your last question, controllers/models/libraries are all classes in that they're all coded in OOP way.
#3

[eluser]codelearn[/eluser]
Kurai -

I actually had to whip up something like this really quickly yesterday:

Using simplepie...

Code:
$count = 1;
foreach($feeds as $url)
{
$this->simplepie->set_feed_url($url);
$this->simplepie->init();
$d['news_rss_'.$count]=$this->simplepie->get_items(0,8);
$count++;
}

$feeds is an array of all the URL's you want to pass. Am I answering the correct question?
#4

[eluser]Kurai[/eluser]
Ok, I'm not sure, then, if I followed the correct path.
Basically what I did was a new library with the class to fetch an RSS, getting only the parameters I really need. This way I could take advantage of simplepie built-in commands:
Code:
class Rss {

    function blogblock($url)
    {
    
        $CI =& get_instance();
        $CI->simplepie->set_feed_url($url);
        $CI->simplepie->init();
        $CI->simplepie->handle_content_type();
        $arr['blogtitle']=$CI->simplepie->get_title();
      
        foreach ($CI->simplepie->get_items(0,1) as $item):
            $arr['itemtitle']=$item->get_title();
            $arr['itemdate']=$item->get_date();
            $arr['itemcont']=word_limiter($item->get_content(), 100);
            $arr['itemplink']=$item->get_permalink();
        endforeach;
        return $arr;
     }

}

Then I called that function from my view, passing the url and constructing the HTML:
Code:
<div id="blog1">
    &lt;?php    
    $rss=$this->rss->blogblock("http://feeds.feedburner.com/kurai");
    ?&gt;
        <h4>&lt;?=$rss['itemdate'];?&gt; on &lt;?=$rss['blogtitle'];?&gt;</h4>
        <h2><a href="&lt;?=$rss['itemplink'];?&gt;" title="&lt;?=$rss['itemtitle'];?&gt;">&lt;?=$rss['itemtitle'];?&gt;</a></h2>
        <div class="content">
        &lt;?=$rss['itemcont'];?&gt; <a href="&lt;?=$rss['itemplink'];?&gt;" title="&lt;?=$rss['itemtitle'];?&gt;">More</a>
        </div>

</div>

I did this mainly because I didn't want to fetch too much data from the RSS, but seeing your advices I'm not so convinced this is the best way to go. What do you think?
If I build an array with all the RSS feed, is there a simple way to retrieve the single elements (say, the post title or the permalink)?
Thank you!
#5

[eluser]NemetraL[/eluser]
In your controller, just do:
Code:
$this->load->library('rss');
$data['rss'] = $this->rss->blogblock("http://feeds.feedburner.com/kurai");
$this->load->view('template_name', $data);

Then in the view (the template_name file):
Code:
<div id="blog1">
&lt;?php foreach ($rss as $val) : ?&gt;
    <h4>&lt;?=$val['itemdate'];?&gt; on &lt;?=$val['blogtitle'];?&gt;</h4>
    <h2><a href="&lt;?=$val['itemplink'];?&gt;" title="&lt;?=$val['itemtitle'];?&gt;">&lt;?=$val['itemtitle'];?&gt;</a></h2>
    <div class="content">
        &lt;?=$val['itemcont'];?&gt; <a href="&lt;?=$val['itemplink'];?&gt;" title="&lt;?=$val['itemtitle'];?&gt;">More</a>
    </div>
&lt;? endforeach; ?&gt;
</div>

Last thing: aren't you retrieving only the last RSS item in your blogblock() function?
#6

[eluser]Kurai[/eluser]
That's right. I set it up this way, for now, but I will accept as a parameter the number of entry to display. This was just for testing purposes.

Another thing: using the way you suggested, how can I set up multiple RSS blocks (each with its URL)? Rightt now the controller doesn't directly load the blog1 view. It loads a container view which composes a header, a footer and several RSS blocks.

The problem is that I don't know how to pass each data to its own block, since it seems that if I call two or more views in the controller only the last is applied...
#7

[eluser]NemetraL[/eluser]
Plenty of posts regarding this issue.. even an entry in the wiki.
It's commonly called 'displaying multiple views'.

Basically, with CI's standard features, you put the partial view in a variable and then you transmit this variable to the final view using:
Code:
$data['blog1'] = $this->view->load('blog1_partial_view', $rss, true);
$this->load->view('template_name', $data);

Other ways of doing it exist: look at Coolfactor's View library for example.
#8

[eluser]Kurai[/eluser]
Thank you very much.
I haven't thought this way to build multiple views, and I was stuck, because with the container method I just can't pass different objects to different views. I go check the wiki and the Coolfactor library.
#9

[eluser]NemetraL[/eluser]
You're welcome.

Happy coding ;-)
#10

[eluser]Kurai[/eluser]
Seems I'm still not so good...
I tried the code you proposed, bu it seems I can't get it to work. It sendss me an error:

Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined property: Aggregator::$view
Filename: controllers/aggregator.php
Line Number: 15
Fatal error: Call to a member function load() on a non-object in /home/.pizza/kurainisei/ataru.urustar.net/system/application/controllers/aggregator.php on line 15

I used this code for the controller:
Code:
$this->load->library("rss");
        $this->load->library('simplepie');
        $rss['source1'] = $this->rss->blogblock("http://feeds.feedburner.com/kurai");
        $rss['source2'] = $this->rss->blogblock("http://feeds.feedburner.com/kuraimh");
        
        $data['blog1'] = $this->view->load('blog1', $rss, true);
        $data['blog2'] = $this->view->load('blog1', $rss, true);
        
        $this->load->view('aggrview' , $data);
    }

Then for the view of a single blog source:
Code:
<div id="blog1">
        <h4>&lt;?=$source1['itemdate'];?&gt; on &lt;?=$source1['blogtitle'];?&gt;</h4>
        <h2><a href="&lt;?=$source1['itemplink'];?&gt;" title="&lt;?=$source1['itemtitle'];?&gt;">&lt;?=$source1['itemtitle'];?&gt;</a></h2>
        <div>
        &lt;?=$source1['itemcont'];?&gt; <a href="&lt;?=$source1['itemplink'];?&gt;" title="&lt;?=$source1['itemtitle'];?&gt;">More</a>
        </div>

</div>

(and I used the exact same code for blog2 view, changing $source1 in $source2)

Finally, the composite view has this code (I omit html and head, it's just the boy content)
Code:
<div class="content">
        &lt;?=$blog1?&gt;
        &lt;?=$blog2?&gt;    
        </div>

Where I am wrong? It seems that the approach of CodeIgniter in this kind of problem is not so obvious... am I forgetting something?

Thank you!




Theme © iAndrew 2016 - Forum software by © MyBB