1 (edited by Peter 2008-01-04 20:36)

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

I'm a non-PHP coder trying to copy/paste a script together, struggling with probably basic stuff. Google is no help. Most tutorials go deep into the PHP mechanisms and jargon with overly fancy examples.

I let registered PunBB users add an RSS feed to their user data. I want to get a list of available RSS feeds from the database and insert it into the SimplePie script that will show the latest users posts on a page.

$query="SELECT rssfeed FROM users";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$rssfeed=mysql_result($result,$i,"rssfeed");

echo "$rssfeed,<br />";

$i++;
}

SimplePie requires something like this:

'http://www.website.com/?feed=rss2',
'http://feeds.feedburner.com/someonesblog',
'http://www.moreblogging.com/my_weblog/atom.xml',

The apostrophes are probably not essential, but the comma apparently is. With the code above I get lots of empty commas, because only a handful of people have submitted RSS feeds so far. How can I get the script to skip empty fields?

The echo output should then replace $memberfeeds in this bit of SimplePie code:

// Initialize some feeds for use.
$feed = new SimplePie();
$feed->set_feed_url(array(
    $memberfeeds
));

How can I do that? This obviously doesn't work:

$memberfeeds=echo "$rssfeed,<br />"

Something like that, but looped so it produces a list of all available RSS feeds.

Again, I couldn't find anything online. It's becoming one of those obsessions that eating up all my time. All suggestions are very welcome, including what keywords to search on and links to good tutorials or examples for these basic things.

Happy New Year!

2 (edited by MattF 2007-12-31 22:54)

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

Something like:

$result = $db->query('SELECT rssfeed FROM '.$db->prefix.'users') or error('Unable to fetch feed information', __FILE__, __LINE__,$db->error());

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

Untested, but should do the job.

3 (edited by Peter 2007-12-31 23:25)

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

Wow MattF, thanks for the quick response, on New Year's Eve! :-)

Going to test it now, will report back...

...I'm back. It's not clear to me how to use this solution. This obviously doesn't work:

$result = $db->query('SELECT rssfeed FROM members') or error('Unable to fetch feed information', __FILE__, __LINE__,$db->error());

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

// Initialize some feeds for use.
$feed = new SimplePie();
$feed->set_feed_url(array(
    $rssfeed
));

I assume I have to replace "simplepie command" with something, but then I'd have to start rewriting/customizing the SimplePie script.

I think your solution would apply "simplepie command" to each RSS feed seperately. I already have a solution that does that, but then all the posts together don't get sorted by date, for example.

I'm looking for a solution that takes the available RSS feeds from the database, puts them in a list (an "array"?), then leaves the rest to the regular Simplepie script.

The regular SimplePie multifeeds script needs something like this:

// Initialize some feeds for use.
$feed = new SimplePie();
$feed->set_feed_url(array(
'http://www.website.com/?feed=rss2', 
'http://feeds.feedburner.com/someonesblog', 
'http://www.moreblogging.com/my_weblog/atom.xml',
));

So how can I get that RSS feed list from the database and instead of echoing it, insert it into SimplePie?

4

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

if ($db->num_rows($result))
{
   $feed_array = array();

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

5

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

OK! And then I put '$feed_array[]' in the SimplePie code where I had '$rssfeeds' ?

I'll try it later. Have to run out to a New Year's Eve party...

Thanks again MattF!

6 (edited by MattF 2007-12-31 23:38)

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

Have just read past the first line or two of your first post. big_smile

You may need to change that query to either:

$db->query('SELECT rssfeed FROM '.$db->prefix."users WHERE rssfeed!=''")

or:

$db->query('SELECT rssfeed FROM '.$db->prefix.'users WHERE rssfeed!=NULL')

7

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

MattF wrote:

Have just read past the first line or two of your first post. big_smile

You may need to change that query to either:

$db->query('SELECT rssfeed FROM '.$db->prefix."users WHERE rssfeed!=''")

or:

$db->query('SELECT rssfeed FROM '.$db->prefix.'users WHERE rssfeed!=NULL')

Right! I've used something like that before. Didn't think of that.

That would solve half of the puzzle. Thanks again! :-)

Gotta run...

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

You can't use = or != for comparing NULL
http://dev.mysql.com/doc/refman/5.0/en/ … -null.html

9

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

Would that then need to be:

$db->query('SELECT rssfeed FROM '.$db->prefix.'users WHERE IS NOT NULL rssfeed')

or

$db->query('SELECT rssfeed FROM '.$db->prefix.'users WHERE rssfeed IS NOT NULL')

or would either achieve the same result?

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

The latter

11

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

Cheers. smile

12

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

This actually worked for me:

$query="SELECT rssfeed FROM users WHERE rssfeed!=''";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$rssfeed=mysql_result($result,$i,"rssfeed");

echo "$rssfeed,<br />";

$i++;
}

It produces a nice output like this:

http://feeds.feedburner.com/website/rss,
http://www.blogblogblog.com/feed/,
http://www.bloggerdyblog.com/feed/,

With 'WHERE rssfeed IS NOT NULL' I still got a list of mostly empty commas. I hadn't set NULL as default for the RSS feed. I've corrected that now, but it seems to have no effect for rows that have already been created. Any way to fill the empty RSS fields with NULL? Is it worth the trouble?

For now I'll stick with what works, moving on to step two, how to insert that output into the SimplePie code. Going to test MattF's earlier suggestion now...

13

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

That one you're using should be fine for both null or empty, I believe. If not, I thing the following should cover both?

$query="SELECT rssfeed FROM users WHERE rssfeed!='' AND rssfeed IS NOT NULL";

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

You should not use NULLs if you can avoid it wink

15

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

I've used this on the database:

UPDATE users SET rssfeed=NULL;

That sets all rssfeed fields to NULL. I had to manually restore two rssfeeds. I should probably have added WHERE rssfeed!='' to the query.

Now the rssfeed IS NOT NULL solution works just as well. Is that the correct way to do it? Is one method better than the other.

Anyway, on to step two (after a late brunch + some coffee)... smile

16

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

Smartys wrote:

You should not use NULLs if you can avoid it wink

Grrr, so I should reverse what I just did...

Thanks though.

17 (edited by Peter 2008-01-01 22:11)

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

I've tried this:

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

$num=mysql_numrows($result);

mysql_close();

if ($db->num_rows($result))
{
   $feed_array = array();

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

// Initialize some feeds for use.
$feed = new SimplePie();
$feed->set_feed_url(array(
$feed_array[]
));

I get this error:

Fatal error: Cannot use [] for reading in...

When I use this, the error disappears, but I get no results on the page:

// Initialize some feeds for use.
$feed = new SimplePie();
$feed->set_feed_url(array(
$feed_array
));

I don't know how to use this solution and am not sure if it will really produce the required result.

With the previous code I can get the list of RSS feeds in a form that SimplePie can use. How can I put the results of that previous code into the SimplePie script, instead of echoing it?

I suspect I have to use return instead of echo and perhaps in the form of a function. This doesn't work, but at least no syntax errors:

$i=0;
while ($i < $num) {
$rssfeed=mysql_result($result,$i,"rssfeed");
$memberfeeds="$rssfeed,<br />";
$i++;
}
return $memberfeeds;

// Initialize some feeds for use.
$feed = new SimplePie();
$feed->set_feed_url(array(
$memberfeeds
));

Am I getting closer to the solution? Any obvious mistakes? Trying to find more info...

18

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

// Initialize some feeds for use.
$feed = new SimplePie();
$feed->set_feed_url($feed_array);

No idea what the simplepie code does with that array, so that may or may not work.

19 (edited by Peter 2008-01-01 22:18)

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

MattF wrote:
// Initialize some feeds for use.
$feed = new SimplePie();
$feed->set_feed_url($feed_array);

No idea what the simplepie code does with that array, so that may or may not work.

Thanks. I've tried this:

if ($db->num_rows($result))
{
   $feed_array = array();

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

// Initialize some feeds for use.
$feed = new SimplePie();
$feed->set_feed_url($feed_array);

SimplePie (presumably) returns this error message:

file_get_contents could not read the file

So I guess this solution delivers the data in a way that SimplePie can't use. That's why I'm trying to find a dumber solution that stays close to that echo output, not an array, but a list like this:

'http://blog.com/rss',
'http://blog2.com/rss',
'http://blog3.com/rss',

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

No, it probably means you have allow_url_fopen off.
Does it work with other links?

21

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

elbekko wrote:

No, it probably means you have allow_url_fopen off.
Does it work with other links?

Yes, SimplePie works fine with this:

// Initialize some feeds for use.
$feed = new SimplePie();
$feed->set_feed_url(array(
'http://blog.com/rss',
'http://blog2.com/rss',
'http://blog3.com/rss',
));

And even with this within code I have:

// Initialize some feeds for use.
$feed = new SimplePie();
$feed->set_feed_url(array(
$rssfeed,
));

But the code I have loops through the available RSS feeds one by one, so they're outputting on the page in the order the script finds them in the database. To take advantage of SimplePie's build-in sort by date I have to hand over a complete list of RSS feeds and leave the rest to SimplePie.

I suspect I have to write a "function" that "returns" the list to SimplePie. Trying to figure out how...

22

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

Other than giving these a try, I've no further suggestions offhand:

$feed->set_feed_url(implode(', ', $feed_array));

or:

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

If not, you'll have to supply more info on the specifics of how Simplepie uses the info supplied.

23 (edited by Peter 2008-01-01 23:14)

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

This doesn't work, but no syntax errors:

function memberfeeds() {

$i=0;
while ($i < $num) {
$rssfeed=mysql_result($result,$i,"rssfeed");
echo "$rssfeed,<br />";
$i++;
}
}

// Initialize some feeds for use.
$feed = new SimplePie();
$feed->set_feed_url(array(
memberfeeds()
));

Do I need to put a return $something in the function?

Thanks for the suggestion MattF. Both solutions produce no results. The file_get_contents error message disappears with the second suggestion:

if ($db->num_rows($result))
{
   $feed_array = array();

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

// Initialize some feeds for use.
$feed = new SimplePie();
$feed->set_feed_url(array(implode(', ', $feed_array)));

I don't want to mess with SimplePie too much. I'm looking for a dumb solution to just hand-over a list of RSS feeds and let SimplePie do the rest.

With your help I now know how to get a correctly formatted list of RSS feeds from the database. Step two is to figure out is how to hand the list over to SimplePie.

24

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

Use that first example I posted, along with one of those two lines above for the feed->set array. You don't need to mess with Simplepie, and if you needed to add anything else that I knew of, I would have already mentioned it.

25 (edited by elbekko 2008-01-01 23:19)

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

$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);

If that doesn't work, do a print_r() of $feed_array.