Re: RSS feeds from database + associated data (skip NULL in MySQL output)
Matt:
1. That is a numerically indexed array, not an comma delineated list of URLs, which is what you are proposing.
2. I wasn't saying that it doesn't allow you to use multiple URLs. We know that works since Peter has done it in this topic already. I was saying that I don't think it allows you to get the most recent post from each URL in the array. That example doesn't seem to do that.
SimplePie does allow to get the most recent post from each URL in a list and then sort by date. I use it in several places in my site and took that code as starting point.
Here's a slightly different version that does the same thing:
<?
// Include the SimplePie library
require_once 'simplepie.inc';
require 'shorten.php';
// Because we're using multiple feeds, let's just set the headers here.
header('Content-type:text/html; charset=utf-8');
// These are the feeds we want to use
$feeds = array(
'http://www.zedshaw.com/feed.atom',
'http://www.usablemarkets.com/?feed=rss2',
'http://feeds.feedburner.com/TheGongShow',
);
// This array will hold the items we'll be grabbing.
$first_items = array();
// Let's go through the array, feed by feed, and store the items we want.
foreach ($feeds as $url)
{
// Use the long syntax
$feed = new SimplePie();
$feed->set_feed_url($url);
$feed->init();
// How many items per feed should we try to grab?
$items_per_feed = 1;
// As long as we're not trying to grab more items than the feed has, go through them one by one and add them to the array.
for ($x = 0; $x < $feed->get_item_quantity($items_per_feed); $x++)
{
$first_items[] = $feed->get_item($x);
}
// We're done with this feed, so let's release some memory.
unset($feed);
}
// We need to sort the items by date with a user-defined sorting function. Since usort() won't accept "SimplePie::sort_items", we need to wrap it in a new function.
function sort_items($a, $b)
{
return SimplePie::sort_items($a, $b);
}
// Now we can sort $first_items with our custom sorting function.
usort($first_items, "sort_items");
// Begin the (X)HTML page.
?>
<?php
$counter=0;
foreach($first_items as $item):
$feed = $item->get_feed();
$counter++;
if($counter>=6){
break;
}
?>
<p>
<strong><a href="<? echo $item->get_permalink(); ?>" target="_blank"><? echo $item->get_title(); ?></strong><br />
<?php unset($feed); ?>
<? echo trim(substr((str_replace("\n", ' ', str_replace("\r", ' ', strip_tags($item->get_description())))),0,80)); ?>
... <i>more</i></a><br />
</p>
<?php
endforeach;
?>
I'll try this version as starting point now, again trying to replace the list of URLs with database results.
With this I get the same result as with the other code; the first ten post of all feeds together, so nine posts from the prolific blogger and one from the lazy one. No luck with the implode versions either.
...
$rss_feeds = array();
while ($data = mysql_fetch_assoc($result))
{
$rss_feeds[] = $data['rssfeed'];
}
// These are the feeds we want to use
$feeds = array(
$rss_feeds
);
// This array will hold the items we'll be grabbing.
$first_items = array();
// Let's go through the array, feed by feed, and store the items we want.
foreach ($feeds as $url)
{
// Use the long syntax
$feed = new SimplePie();
$feed->set_feed_url($url);
$feed->init();
// How many items per feed should we try to grab?
$items_per_feed = 10;
...