RSS displaying multiple feeds in time/date published order

Ask about a PHP problem here.
Post Reply
dareskog
Posts: 12
Joined: Sat Jan 21, 2012 2:49 am

RSS displaying multiple feeds in time/date published order

Post by dareskog »

I'm trying to create a php rss parser that takes on multiple feeds, takes 10 entries from each, these all then get mixed together in an array and displayed in date order.
Atm i've got this:
<?php
require_once 'magpierss/rss_fetch.inc';
$num_items = 10;
$urls = array( "http://url1.com", "http://url2.com", "http://url3.com" );

foreach ($urls as $url) {
	$rss = fetch_rss($url);
	$items = array( $rss );
	$items = array_slice($rss->items, 0, $num_items);
	
		foreach ( $items as $item ) {
			$title = $item[title];
			$url = $item[link];
			echo "<a href=$url>$title</a><br />";
		}	
}
?>
The only bit I've got left to do is have them displayed in a collaborated date order but don't know how to get this done :(. I'm a new comer to php and have gone nuts trying to sort this out, so any help/examples would be awesome!
Cheers =]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: RSS displaying multiple feeds in time/date published ord

Post by jacek »

Well one thing, you can remove this line
$items = array( $rss );
since the one below it will overwrite it.

As for the sorting thing, you need to read all of the article you are going to show into one array and they sort that. So your code will probably look more like this
<?php
require_once 'magpierss/rss_fetch.inc';

$num_items = 10;
$urls = array( "http://url1.com", "http://url2.com", "http://url3.com" );

$items = array();

foreach ($urls as $url) {
    $rss = fetch_rss($url);
    $items = array_merge($items, array_slice($rss->items, 0, $num_items));
}

// do sorting on $items

foreach ( $items as $item ) {
     $title = $item[title];
     $url = $item[link];
     echo "<a href=$url>$title</a><br />";
}
?>
For the sorting then you probably need to use a usort(), have a look at http://php.net/usort for some examples.
Image
dareskog
Posts: 12
Joined: Sat Jan 21, 2012 2:49 am

Re: RSS displaying multiple feeds in time/date published ord

Post by dareskog »

Ah cheers! I was able to work out a sorting method using the link you provided, took me a while to get it tho!
I've just got one last question which I'm struggling with.
ATM, I've got this:
$num_items = 8;
$urls = array( "http://www.url1.com", "http://www.url2.com", "http://www.url3.com" );

$items = array();
foreach ($urls as $url) {
    $rss = fetch_rss($url);
    $items = array_merge($items, array_slice($rss->items, 0, $num_items));
}

usort($items, 'date_cmp');

foreach ( $items as $item ) {
     $title = $item[title];
     $url = $item[link];
     echo "- <a href=$url>$title</a><br />";
}

function date_cmp($a, $b) 
{
$atime = (empty($a['date_timestamp']))
? strtotime($a['dc']['date'])
: $a['date_timestamp'];

$btime = (empty($b['date_timestamp']))
? strtotime($b['dc']['date'])
: $b['date_timestamp'];

if ($atime == $btime)
return 0;

return ($atime > $btime) ? -1 : 1;
}
My Question is, is there any way of identifying from the individual rss outputs which url they originated from?
So this could then lead me on to being able to somehow detect which rss output came from which url rss feed and therefore displaying a corresponding picture.
E.G say one of the rss feeds was (i know this isn;t a real rss feed btw lol) www.google.com/rss, for each rss feed that got outputed originating from this url would have the google favicon next to it.
Is this possible?
Cheers again =]
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: RSS displaying multiple feeds in time/date published ord

Post by jacek »

It might be, the best thing to do would be to look at the source of the feed and see if they all have a property in their header that states the url. I don't think it is a required filed for RSS but they might all have it.
Image
Post Reply