RSS organized by date/time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

RSS organized by date/time

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: RSS organized by date/time

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: RSS organized by date/time

Post 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";
}
 
?>
Last edited by divito on Thu Mar 12, 2009 9:55 am, edited 1 time in total.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: RSS organized by date/time

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: RSS organized by date/time

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: RSS organized by date/time

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
divito
Forum Commoner
Posts: 89
Joined: Sun Feb 22, 2009 7:29 am

Re: RSS organized by date/time

Post by divito »

Alright, it took some trial and error, but I got it to work. Thanks a lot.
Post Reply