26 (edited by Peter 2008-01-01 23:35)

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

elbekko wrote:
$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.

Thanks. It doesn't produce results. Not sure how to "do a print_r()". I've tried this:

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

$memberfeeds = print_r($feed_array);

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

Only produces "Array ( )" in the page, which was to be expected.

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

Well, that'd mean the array ($feed_array) is empty.

28

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

elbekko wrote:

Well, that'd mean the array ($feed_array) is empty.

It shouldn't be. There are two RSS feeds in the database that do show up with some of the other variations I've shown in this thread.

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

Paste the full code you're using

30 (edited by Peter 2008-01-02 01:12)

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

Here's the full code that I use on my site now. It works, but the script loops through each available RSS feed sepeately. I'm looking for an alternative that hands a complete list of feeds over to SimplePie, so SimplePie can sort the results by date:

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

$num=mysql_numrows($result);

mysql_close();

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

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

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

// Initialize the feed object
$feed->init();
 
// This will work if all of the feeds accept the same settings.
$feed->handle_content_type();

?> 
    <?php if ($feed->error): ?>
        <p><?=$feed->error()?></p>
    <?php endif ?>

    <?php
// Let's loop through each item in the feed.
    foreach($feed->get_items(0,1) as $item):

    // Let's give ourselves a reference to the parent $feed object for this particular item.
    $feed = $item->get_feed();
?>

<h3><a href="<?php echo $item->get_permalink(); ?>" target="_blank"><?php echo html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8'); ?></a></h3>

<!-- get_content() prefers full content over summaries -->
<?php echo $item->get_description();
?>
<p class="footnote"><a href="<?php echo $feed->get_permalink(); ?>" target="_blank"><?php echo $feed->get_title(); ?></a> | <?php echo $item->get_date('M j, Y | g:i a'); ?></p><br />

<?php endforeach;
$i++;
}
?>

My original question was how to insert echo output elsewhere in the same PHP script, because I know how to output the list I need. Or is that impossible? I'm not sure the array approaches work with SimplePie.

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

Try this

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

$rss_feeds = array();
while ($data = mysql_fetch_assoc($result))
{
    $rss_feeds[] = $data['rssfeed'];
}

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

// Initialize the feed object
$feed->init();
 
// This will work if all of the feeds accept the same settings.
$feed->handle_content_type();

?> 
    <?php if ($feed->error): ?>
        <p><?=$feed->error()?></p>
    <?php endif ?>

    <?php
// Let's loop through each item in the feed.
    foreach($feed->get_items(0,1) as $item):

    // Let's give ourselves a reference to the parent $feed object for this particular item.
    $feed = $item->get_feed();
?>

<h3><a href="<?php echo $item->get_permalink(); ?>" target="_blank"><?php echo html_entity_decode($item->get_title(), ENT_QUOTES, 'UTF-8'); ?></a></h3>

<!-- get_content() prefers full content over summaries -->
<?php echo $item->get_description();
?>
<p class="footnote"><a href="<?php echo $feed->get_permalink(); ?>" target="_blank"><?php echo $feed->get_title(); ?></a> | <?php echo $item->get_date('M j, Y | g:i a'); ?></p><br />

<?php endforeach;
$i++;
}

mysql_close();

?>

32

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

Thanks Smartys.

After removing this:

$i++;
}

It looks promising, but I get only post(s) from one feed.

I think SimplePie doesn't handle an array the same as a dumb list with '...', Or should they really be the same thing?

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

It was handing an array before, when you were passing it one at a time as an array. It handles multiple URLs.
However, your code is getting one item total from all feeds (that's get_items(0, 1)).

34 (edited by Peter 2008-01-02 02:04)

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

Smartys wrote:

It was handing an array before, when you were passing it one at a time as an array. It handles multiple URLs.
However, your code is getting one item total from all feeds (that's get_items(0, 1)).

No, I've tried changing the 1 to 3. It then shows 3 items from the same 1 feed.

Why doesn't something like this work?:

function memberfeeds() {
$num=mysql_numrows($result);

mysql_close();

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

$memberfeeds = memberfeeds();

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

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

You're echoing data there, not storing strings in an array. Also, there's no query there.
If it's ordered by date as you say, why couldn't the first three most recent entries be from one feed?

36 (edited by Peter 2008-01-02 02:40)

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

Smartys wrote:

You're echoing data there, not storing strings in an array. Also, there's no query there.
If it's ordered by date as you say, why couldn't the first three most recent entries be from one feed?

Good point. When I try the first 10, one post from the second feed does indeed show up.

It's definitely progress! smile

But it's not exactly what I wanted or expected. I want one post from each feed and then sort by date. That's what the script does when I add a list of RSS feeds manually. That's what I'm trying to replicate with feeds from the database.

In my latest "solution" the query isn't the problem, it's above the code I showed. Not storing strings in an array, yes, I'm not sure an array is what I need. I'm trying to replicate a list of feeds like I normally use.

I know echo-ing doesn't work. My original question was, is it possible to insert that output elsewhere in a PHP script, instead of echo-ing. The array approach apparently does not produce the same result as the normal '...', list of feeds.

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

I think you would be better off asking people more familiar with SimplePie wink

The way to handle it may be to pass 1 URL per call (as you were doing), store all the data in an array, and then sort the data from there.

38 (edited by Peter 2008-01-02 04:15)

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

Smartys, my original question was about inserting output from one part of a PHP script elsewhere in the same script, instead of echo-ing it. I should never have brought up SimplePie! I've been saying all along that I don't want to go into SimplePie, which works fine elsewhere in my site.

How can I put a formatted list of database output elsewhere in a PHP script? Should I look into creating a function for it? Use return instead of echo in some way? Or am I totally on the wrong track?

How would I store strings in an array in my attempt at #34? Is there any way to make that approach work? Or would it come down to the same thing?

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

You wouldn't echo, you would assign the values to an array and use that array elsewhere. You could do it within a function but you don't have to (and yes, you would have to return your array if you choose to use a function).

40 (edited by Peter 2008-01-02 06:16)

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

Does the output have to be assigned to an array before you can use it elsewhere in the script? Is there no way to assign it as a dumb "text string" (?) to something like $memberfeeds?

41 (edited by MattF 2008-01-02 10:39)

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

Peter wrote:

Smartys, my original question was about inserting output from one part of a PHP script elsewhere in the same script, instead of echo-ing it. I should never have brought up SimplePie! I've been saying all along that I don't want to go into SimplePie, which works fine elsewhere in my site.

No, your original question was regarding Simplepie and RSS feeds.

Just look at the code you've already been given. With the array you create, you can do what the chuff you like with it later, whether that's using it in a foreach loop and echoing the content, sorting, splitting or whatever else takes your fancy.

42

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

MattF, you don't have to tell me what my original question was. It's even there in the title.

Sure, the problem I was trying to solve had to do with SimplePie and I gave the code to illustrate why I asked my question. I very much appreciate you've tried to help me solve the problem, but it wasn't the original question I repeatedly tried to return to in this thread.

My question was whether it's possible to insert/use results you can echo elsewhere in a PHP script. That may be dumb irrelevant question to you, but as a non-PHP coder I still have to figure out the basics.

With your array solutions the SimplePie scripts behaves very different than when I put in a regular list of feeds. They're not the same thing. I don't know how to work with arrays, so I'm at a new dead end if I have to start editing how SimplePie code handles arrays.

I've already wasted four days on this. I give up.

43

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

The question you keep repeating is moot. You have all the info you need in this thread to do exactly what you require. Note my last post, regarding the array and a foreach loop. And if Simplepie was not the point of this thread, then why mention this:

With your array solutions the SimplePie scripts behaves very different than when I put in a regular list of feeds. They're not the same thing. I don't know how to work with arrays, so I'm at a new dead end if I have to start editing how SimplePie code handles arrays.

Seriously, the problem is not with any of the code which has been supplied to you, but with something else. However, if you wish to prove the point, use that first array code I posted, and then use this to echo each line individually, (providing that rssfeed is the only content being put into each line in the array):

foreach ($feed_array as $value)
{
    echo $value."\n";
}

The problem is that you have your mind set upon a specific answer you want, rather than the possibility of a better solution.

44

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

Thanks. I do appreciate the help. This still doesn't work though:

$rss_feeds = array();
while ($data = mysql_fetch_assoc($result))
{
    $rss_feeds[] = $data['rssfeed'];
}

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

I get this error message:

A feed could not be found at 
http://feeds.feedburner.com/communitywireless/rss, 
http://www.leveragingideas.com/feed/

Those are the actual feeds. They exist and should work fine.

Adding this (I've tried different variations) only brings me back to echo-ing the feed addresses on the page:

foreach ($rss_feeds as $value)
{
    echo $value."\n";
}

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

You don't want to implode. set_feed_url takes either a string, which is one URL, or an array, which is multiple URLs.
Now, you want to get the most recent entry from each feed and then order them by date, right? If so, I don't believe SimplePie allows you to do that just using its functions. Doing that involves doing what you were doing (calling each feed URL one by one), storing the results for each feed together in an array, and then sorting those results.

46 (edited by MattF 2008-01-02 14:50)

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

Just out of curiosity, change this line:

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

to:

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

or:

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

I believe that first syntax should be correct for creating/keeping the single-quotes, but may be wrong.

Edit: I've just edited that code above for the array bit. Hadn't noticed the lack of it until Smartys mentioned. big_smile

47

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

Smartys wrote:

If so, I don't believe SimplePie allows you to do that just using its functions. Doing that involves doing what you were doing (calling each feed URL one by one), storing the results for each feed together in an array, and then sorting those results.

It would appear it does, by the looks of it. From the Simplepie docs:

// Multiple feeds
$feed = new SimplePie();
$feed->set_feed_url(array(
    'http://simplepie.org/blog/feed/',
    'http://digg.com'
));
$feed->enable_order_by_date(false);
$feed->cache_location($_SERVER['DOCUMENT_ROOT'] . '/cache');
$feed->init();
echo $feed->get_title();

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. Or rather, an array with one entry which is a comma delineated list of URLs.
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.

49

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

MattF wrote:

Just out of curiosity, change this line:

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

to:

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

or:

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

I was curious about this, but wasn't sure how to do it. So thanks!

No results. No syntax errors either. I've tried different variations. Sometimes I get 'A feed could not be found at...'

50

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.

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.

smartys wrote:

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.

My apologies. I misinterpreted that one. smile