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.

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

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

Still no results with:

$ix = 0;

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

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

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?

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!

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

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?

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

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";
}

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.

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?

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?

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.

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

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?

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.

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.

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.

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.

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

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',

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

Smartys wrote:

You should not use NULLs if you can avoid it wink

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

Thanks though.