Page 1 of 1

RSS organized by date/time

Posted: Wed Mar 11, 2009 2:31 pm
by divito
I'm using magpierss and have been able to setup and format all the RSS feeds and their outputs to the way I want them for a page. However, that is all as separate feeds in separate tables.

That I've grasped and garnered what I wanted out of that, I'm curious as to a way I can implement all the RSS feeds into one table, and have them organized by date/time, so that no matter which feed and site it is pulling it from, they would be posted in descending order.

I had first hoped I could perhaps put all the URLs into an array so I could utilize the single foreach, but I didn't have much luck. I'm not even sure magpie can deal with that. I had a few other ideas, but I'd have to spend some more time reading to see if they'd work.

Any hints or suggestions?

Re: RSS organized by date/time

Posted: Wed Mar 11, 2009 2:34 pm
by pickle
RSS is a standard format, so the type of data you get out of each RSS feed should be identical. Each of your tables should have columns for each possible type of data that can exist in a single RSS entry. When you download & parse an RSS feed, put each entry in 1 table, rather than it's own table.

Re: RSS organized by date/time

Posted: Thu Mar 12, 2009 9:33 am
by divito
For the following, ignoring that I'm not formatting the output just yet, what kind of if statement would set it up as posting the newest ones first??

Code: Select all

<?php
 
 
require_once 'magpierss-0.72/rss_fetch.inc';
 
$url = 'http://www.mymym.com/en/rss/news/all.xml';
$url2 = 'http://www.gotfrag.com/portal/xact/xml/?f=stories&a=any';
$rss = fetch_rss($url);
$rss2 = fetch_rss($url2);
 
foreach ($rss->items as $item ) {
    $title = $item[title];
    $url   = $item[link];
    $date  = $item[pubdate];
    
    echo "$date: <a href=$url>$title</a></li><br>\n";
}
 
foreach ($rss2->items as $item ) {
    $title = $item[title];
    $url   = $item[link];
    $date  = $item[pubdate];
    
    echo "$date: <a href=$url>$title</a></li><br>\n";
}
 
?>

Re: RSS organized by date/time

Posted: Thu Mar 12, 2009 9:52 am
by pickle
You're outputing both RSS feeds separately. Put them both into one array, sort it by date, then output that array. That'll help with a separation of business/display logic as well.

Re: RSS organized by date/time

Posted: Thu Mar 12, 2009 10:21 am
by divito
I planned on using an array for the feeds, which I've done now to make it a little neater.

Code: Select all

<?php
 
require_once 'magpierss-0.72/rss_fetch.inc';
 
$feeds = array ('http://www.mymym.com/en/rss/news/all.xml', 'http://www.gotfrag.com/portal/xact/xml/?f=stories&a=any', 'http://www.sk-gaming.com/rss/');
 
foreach ($feeds as $feed ) {
    $rss = fetch_rss($feed);
 
foreach ($rss->items as $item ) {
    $title = $item[title];
    $url   = $item[link];
    $date  = $item[pubdate];
    
    echo "$date: <a href=$url>$title</a></li><br>\n";
}
}
?>
My guess is that I would need to create the array and add each entry to the array using array_push within the first foreach, and maybe even use array_combine to separate the titles/links with their respective dates? I'm not at all adept with arrays enough to the point where I can do as you suggest off the top of my head.

Rather than plant down the exact code I'd need, perhaps throwing down the functions I might use and I'll trial my way through it until it works?

Re: RSS organized by date/time

Posted: Thu Mar 12, 2009 10:28 am
by pickle
You don't need a special array function to do it:

Code: Select all

foreach ($rss->items as $item ) {
    $date  = $item['pubdate'].microtime(TRUE);
 
    $entries[$date] = $item;
}
 
krsort($entries);
First you put all the rss items in a single array. I've used the item publish date appended to the current time with microseconds (to ensure no item gets overwritten) as the key, then sorted by the key. After that code is run, $entries should contain all entries from most to least recent.

To put multiple feeds into the one array, just run that foreach() loop with $rss being different each time.

Re: RSS organized by date/time

Posted: Thu Mar 12, 2009 8:07 pm
by divito
Alright, it took some trial and error, but I got it to work. Thanks a lot.