51 (edited by Peter 2008-01-02 15:14)

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

Smartys wrote:

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;
...

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

The code I've given should work providing you add the correct query.

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

MattF wrote:
Smartys wrote:

Matt:
1. That is a numerically indexed array, not an comma delineated list of URLs, which is what you are proposing.

The Simplepie array or the one we've created? Judging by their docs, the code for creating that array I posted earlier and then using the implode inside their array should give output which is similar to that in their documentation for the set_feed_url.

No. Your code is wrong. wink
Their code:

$feed->set_feed_url(array(
    'http://simplepie.org/blog/feed/',
    'http://digg.com'
));

That's an array with two elements. The array is numerically indexed, so the key 0 points to http://simplepie.org/blog/feed/ and key 1 points to http://digg.com.

Your code:

$feed->set_feed_url(array("'".implode("', '", $rss_feeds)."'"));

or:

$feed->set_feed_url(array(implode(', ', $rss_feeds)));

That's a numerically indexed array with one element. So key 0 points to a comma delineated list of URLs.

The two are NOT equivalent.

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

Peter: the code you just posted does almost EXACTLY what I told you to do. tongue
I'll write up a version that works, one second wink

55

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

elbekko wrote:

The code I've given should work providing you add the correct query.

Thanks elbekko.

I tried this. It doesn't do anything:

<?php
// Include the SimplePie library
require_once 'simplepie.inc';
require 'shorten.php';
require_once 'idn/idna_convert.class.php';

mysql_connect(localhost,$db_username,$db_password);
@mysql_select_db($db_name) or die( "Unable to select database");

$query="SELECT id, firstname, rssfeed FROM members WHERE rssfeed!=''";
$result=mysql_query($query);

$feed_array = array();
if ($db->num_rows($result))
{
   while ($cur_feed = $db->fetch_assoc($result))
       $feed_array[] = $cur_feed['rssfeed'];
}

$feed = new SimplePie($feed_array);

// Initialize the feed object
$feed->init();
 
...

Am I missing something?

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

<?
// 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
mysql_connect(localhost,$db_username,$db_password);
@mysql_select_db($db_name) or die( "Unable to select database");

$query="SELECT id, firstname, rssfeed FROM members WHERE rssfeed!=''";
$result=mysql_query($query);

$feeds = array();
while ($cur_feed = mysql_fetch_assoc($result))
    $feeds[] = $cur_feed['rssfeed'];
 
// 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;
?>

There, that should work based on what you just gave me.

57

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

It does!! smile

Thanks Smartys!! smile

I'm sorry I put you all through this and that I wasn't able to figure it out by myself. But the solution is now here for everybody and I think it's a pretty cool feature for any community site (like PunBB), to be able to promote your members' latest personal blog posts.

smile

58

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

Smartys wrote:

That's a numerically indexed array with one element. So key 0 points to a comma delineated list of URLs.

The two are NOT equivalent.

My apologies. I never realised until you just explained it that implode doesn't actually split them. That's one I'll remember for future reference. Explains why that code I posted was buggered too. big_smile Thanks for the explanation. smile

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

Matt: I think you confused implode with explode wink
Peter: Don't worry about it, I'm glad we found a solution wink

60

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

Smartys wrote:

Matt: I think you confused implode with explode wink

Possibly. smile It's not an error I'll make a second time though, thanks to your correction. They will go giving these functions similar names and confusing people. big_smile

61 (edited by Peter 2008-01-03 01:11)

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

SimplePie today released a new version (1.1) with what I needed "built in", sort of. I've tested it with the second version of the code I put in my post there. (I still wouldn't have figured out the array thing on my own...)

Embarrassingly I'm still struggling with one more problem before I can use the script. I can't figure out how to output some associated data (id, username) with the feeds.

The old version had a block like this that did the trick:

$rssfeed=mysql_result($result,$i,"rssfeed");
$id=mysql_result($result,$i,"id");
$firstname=mysql_result($result,$i,"firstname");
$username=mysql_result($result,$i,"username");

I thought it would be easy to do in the new version. The closest I got to some type of result was with this:

$query="SELECT id, firstname, username, rssfeed FROM members WHERE rssfeed!=''";
$result=mysql_query($query);

$feeds = array();
while ($cur_feed = mysql_fetch_assoc($result))
        $feeds[] = $cur_feed['rssfeed'];
    $id = mysql_result($result,$cur_feed,"id");
    $firstname = mysql_result($result,$cur_feed,"firstname");
    $username = mysql_result($result,$cur_feed,"username");

But it attaches the user data of the first feed to all posts/feeds. sad

I've wasted hours again today trying all sorts of combinations and variations. I'm not asking you to do the same, waste your time and do my work for me, but if anyone simply knows the solution, please help!

I hope (and suspect) it's something obvious and easy to fix.

Thanks!

62 (edited by MattF 2008-01-03 04:27)

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

Simplest way, I believe, is to create arrays for each item, i.e:

$feeds = array();
$id_array = array();

while ($cur_feed = mysql_fetch_assoc($result))
{
        $feeds[] = $cur_feed['rssfeed'];
        $id_array[] = $cur_feed['id'];
  .....................etc, etc

then in the foreach loop do along the lines of:

$user_id = $id_array[$ix];
$ix++;

or echo or whatever you want to do with it.

63 (edited by Peter 2008-01-03 16:00)

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

Thanks MattF. This looks logical to me, but doesn't do anything:

$query="SELECT id, firstname, username, rssfeed FROM members WHERE rssfeed!=''";
$result=mysql_query($query);

$feeds = array();
$id_array = array();
$first = array();

while ($cur_feed = mysql_fetch_assoc($result))
    $feeds[] = $cur_feed['rssfeed'];
    $id_array[] = $cur_feed['id'];
    $first[] = $cur_feed['firstname'];
...
foreach ($feeds as $url)
{
    $user_id = $id_array[$ix];
    $ix++;

    $firstname = $first[$ix];
    $ix++;
...

I've also tried this:

    for ($x = 0; $x < $feed->get_item_quantity($items_per_feed); $x++)
    {
        $first_items[] = $feed->get_item($x);

        $user_id = $id_array[$ix];
        $ix++;

        $firstname = $first[$ix];
        $ix++;
    }

Should I use mysql_fetch_row or another variation?

64 (edited by MattF 2008-01-03 15:58)

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

If I remember correctly offhand, fetch_assoc should be fine. I forgot to mention, btw, remember to set

$ix = 0;

initially outside of the foreach loop. big_smile

65 (edited by MattF 2008-01-03 16:04)

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

Peter wrote:

I've also tried this:

    for ($x = 0; $x < $feed->get_item_quantity($items_per_feed); $x++)
    {
        $first_items[] = $feed->get_item($x);

        $user_id = $id_array[$ix];
        $firstname = $first[$ix];
        $ix++;
    }

Should I use mysql_fetch_row or another variation?

You only want that var incremented once inside the loop, btw, as above.

Edit: In fact, if you're only wanting that info inside of the same loops as the $x count, you can ditch the $ix altogether and just use the $x var.

66

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

Still no results with:

$ix = 0;

foreach ($feeds as $url)
{
        $user_id = $id_array[$ix];
        $ix++;

        $firstname = $first[$ix];
        $ix++;

67 (edited by Peter 2008-01-03 16:10)

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

MattF wrote:

...
Edit: In fact, if you're only wanting that info inside of the same loops as the $x count, you can ditch the $ix altogether and just use the $x var.

Yes, that's what I want. The feeds are the array, the id, username etc. are just along for the ride. There should be short way to achieve that.

This didn't do anything either:

$ix = 0;

$user_id = $id_array[$ix];
$ix++;

$firstname = $first[$ix];
$ix++;

foreach ($feeds as $url)
{ ...

But as you see I have no clue what I'm doing. sad

Using $x var would look something like this?

$user_id = $id_array[$ix var];

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

$query="SELECT id, firstname, username, rssfeed FROM members WHERE rssfeed!=''";
$result=mysql_query($query);

$feeds = array();
$id_array = array();
$first = array();

while ($cur_feed = mysql_fetch_assoc($result))
{
    $feeds[] = $cur_feed['rssfeed'];
    $id_array[] = $cur_feed['id'];
    $first[] = $cur_feed['firstname'];
}
...
foreach ($feeds as $url)
{
    $user_id = $id_array[$ix];
    $ix++;

    $firstname = $first[$ix];
    $ix++;
...

69

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

I had already tried that one, Elbekko. Thanks though.

70

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

elbekko wrote:
foreach ($feeds as $url)
{
    $user_id = $id_array[$ix];
    $ix++;

    $firstname = $first[$ix];
    $ix++;
...

That last bit only needs one increment, to keep the relationship intact:

foreach ($feeds as $url)
{
    $user_id = $id_array[$ix];
    $firstname = $first[$ix];
    $ix++;

71

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

Peter wrote:

I had already tried that one, Elbekko. Thanks though.

Just post the code you already have to hand. It'll be far less painful that way. big_smile

72

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

This is my latest attempt, taking all suggestions (thanks so much!) into account:

$query="SELECT id, firstname, username, rssfeed FROM members WHERE rssfeed!=''";
$result=mysql_query($query);

$feeds = array();
$id_array = array();
$first = array();

while ($cur_feed = mysql_fetch_assoc($result))
    $feeds[] = $cur_feed['rssfeed'];
    $id_array[] = $cur_feed['id'];
    $first[] = $cur_feed['firstname'];

// 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)
{
    $user_id = $id_array[$ix];
    $firstname = $first[$ix];
    $ix++;

    // Use the long syntax
    $feed = new SimplePie();
    $feed->set_feed_url($url);
    $feed->init();

It looks sensible to me, but no results. I suspect the approach is still wrong somehow. id, username etc. should be "along for the ride" with the rssfeeds and this approach seems to put them on the same level. Something feels wrong there.

73

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

Peter wrote:
$query="SELECT id, firstname, username, rssfeed FROM members WHERE rssfeed!=''";
$result=mysql_query($query);

$feeds = array();
$id_array = array();
$first = array();

while ($cur_feed = mysql_fetch_assoc($result))
    $feeds[] = $cur_feed['rssfeed'];
    $id_array[] = $cur_feed['id'];
    $first[] = $cur_feed['firstname'];

// 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.

$ix = 0;

foreach ($feeds as $url)
{
    $user_id = $id_array[$ix];
    $firstname = $first[$ix];
    $ix++;

    // Use the long syntax
    $feed = new SimplePie();
    $feed->set_feed_url($url);
    $feed->init();

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

The while is still wrong.

75

Re: RSS feeds from database + associated data (skip NULL in MySQL output)

I give up for now. I really have to put some time in my day job...

Thanks for all the suggestions! smile