Re: RSS feeds from database + associated data (skip NULL in MySQL output)
The while is still wrong.
I'd never noticed that.
You are not logged in. Please login or register.
PunBB Forums → Programming → RSS feeds from database + associated data (skip NULL in MySQL output)
The while is still wrong.
I'd never noticed that.
I give up for now.
Well stop stripping parts off the code you're given then.
$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();
Aarrgg, it's an obsession!
Copying MattF's latest code I get a result similar to my first attempt; id, username etc. associated with one feed shows up on all posts. In this case it's the id, username from the top/latest post.
With this I get the id, username from the first row in the database again, showing up on all posts:
// 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);
$user_id = $id_array[$x];
$firstname = $first[$x];
$x++;
}
Here's a link to see MattF's suggestion in action. The live version is members only, but I've now removed that code from the test.php. The posts are from two different feeds, but get the same id etc.
for ($x = 0; $x < $feed->get_item_quantity($items_per_feed); $x++) { $first_items[] = $feed->get_item($x); $user_id = $id_array[$x]; $firstname = $first[$x]; $x++; }
You have already set user_id and firstname at the top of the loop. You shouldn't have them in that loop too. All you are achieving there is running through the array without doing anything with the info. They're already set. Echo the output where you need it.
... You have already set user_id and firstname at the top of the loop. You shouldn't have them in that loop too. All you are achieving there is running through the array without doing anything with the info. They're already set. Echo the output where you need it.
I was trying that as an alternative, so without also setting user_id, firstname at the top of the loop. The result was slightly different, but still the same id etc. on all posts.
The version here is copied straight from your post. It has the same problem.
Post the complete script you're using.
Post the complete script you're using.
<?
// 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, username, rssfeed FROM members WHERE rssfeed!=''";
$result=mysql_query($query);
$feeds = array();
$id_array = array();
$first = array();
$user = array();
while ($cur_feed = mysql_fetch_assoc($result))
{
$feeds[] = $cur_feed['rssfeed'];
$id_array[] = $cur_feed['id'];
$first[] = $cur_feed['firstname'];
$user[] = $cur_feed['username'];
}
// 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];
$username = $user[$ix];
$ix++;
// 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");
$counter=0;
foreach($first_items as $item):
$feed = $item->get_feed();
$counter++;
if($counter>=10){
break;
}
if ($img_size = @getimagesize($pun_config['o_avatars_dir'].'/'.$id.'.jpg'))
$avatar_field = '<img src="'.$pun_config['o_avatars_dir'].'/'.$id.'.jpg" alt="" />';
else
$avatar_field = '<img src="'.$pun_config['o_avatars_dir'].'/nopicture.png" alt="" />';
$picture = '<div id="feedpicture"><a href="member.php?id='.$user_id.'" title="'.$firstname.'\'s Member Page">'.$avatar_field.'</a></div>';
// Begin the (X)HTML page.
?>
<h3><?php echo $picture; ?><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 -->
<? echo trim(substr((str_replace("\n", ' ', str_replace("\r", ' ', strip_tags($item->get_description())))),0,300)); ?>
... <i>more</i></a><br />
<p class="footnote"><a href="member.php?id=<?php echo $user_id; ?>" title="<?php echo $firstname.'\'s Member Page'; ?>"><?php echo $username; ?></a> | <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 will stress very highly about this, multi-dimensional arrays *ARE NOT* my strongpoint, so this script may be completely flawed. I would honestly suggest not testing it until one of the other, more learned ones, have given it a once over at least. But, here is my claim for ridicule, :
<?php
// 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, username, rssfeed FROM members WHERE rssfeed!=''";
$result=mysql_query($query);
$feeds = array();
$i = 0;
$xf = 0;
$xi = 0;
$xx = 0;
while ($cur_feed = mysql_fetch_assoc($result))
{
$feeds[$i][uid] = $cur_feed['id'];
$feeds[$i][feed] = $cur_feed['rssfeed'];
$feeds[$i][fname] = $cur_feed['firstname'];
$feeds[$i][uname] = $cur_feed['username'];
$i++;
}
// 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.
while ($xi <= $i)
{
$url = $feeds[$xi][feed];
// 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[$xx][feed] = $feed->get_item($x);
$first_items[$xx][uid] = $feeds[$xi][uid];
$first_items[$xx][fname] = $feeds[$xi][fname];
$first_items[$xx][uname] = $feeds[$xi][uname];
$xx++;
}
$xi++;
// 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");
*/
$counter = 0;
while ($xf <= $xx)
{
$item = $first_items[$xf][feed];
$user_id = $first_items[$xf][uid];
$firstname = $first_items[$xf][fname];
$username = $first_items[$xf][uname];
$feed = $item->get_feed();
$counter++;
$xf++;
if ($counter >= 10)
{
break;
}
if ($img_size = @getimagesize($pun_config['o_avatars_dir'].'/'.$user_id.'.jpg'))
{
$avatar_field = '<img src="'.$pun_config['o_avatars_dir'].'/'.$user_id.'.jpg" alt="" />';
}
else
{
$avatar_field = '<img src="'.$pun_config['o_avatars_dir'].'/nopicture.png" alt="" />';
}
$picture = '<div id="feedpicture"><a href="member.php?id='.$user_id.'" title="'.$firstname.'\'s Member Page">'.$avatar_field.'</a></div>';
// Begin the (X)HTML page.
?>
<h3><?php echo $picture; ?><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 -->
<? echo trim(substr((str_replace("\n", ' ', str_replace("\r", ' ', strip_tags($item->get_description())))),0,300)); ?>
... <i>more</i></a><br />
<p class="footnote"><a href="member.php?id=<?php echo $user_id; ?>" title="<?php echo $firstname.'\'s Member Page'; ?>"><?php echo $username; ?></a> | <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
}
?>
Seriously though, regarding the caution. I would wait until it's been checked over first. It could do anything from literally nothing, upto going into a constant while loop.
Edit: The sorting is also disabled, as that would have reset all the keys.
Thanks so much MattF! I obviously know far less about multi-dimensional arrays.
I did try the code. It doesn't do anything unfortunately. Only one post shows up, with any related id, username etc.
Apologies again I suck at this myself, but the complete end result would be useful to many people.
On my site (test version of the page) a third blogger now added his RSS feed.
I'll pay $20 for the first fully working solution coming in, assuming the solution is simple for any experienced PHP-coder. If it's more complicated, more compensation is negotiable. I'm not rich...
Starting point is the script at post #82, which you can see in action here. I need the correct id, username and other data to show up with each feed. In the test version posts from different feeds all get the same id, username etc.
You should be able to reach me through this forum's internal mail system. Or try info at web2newyork.
(This forum should have a job section or even a more elaborate programming job trading system, with paypal etc.)
I've sent a mail
I've edited that code I posted above. Not sure if it'll make any difference, but give it a whirl.
Edit: I've just given it a second edit to move the $xi lower in the loop.
I've edited that code I posted above. Not sure if it'll make any difference, but give it a whirl.
Edit: I've just given it a second edit to move the $xi lower in the loop.
Thanks MattF!
I've just tried it. It gives a weird mixed result. It screws up the layout, CSS is gone. So far that happened with this page whenever I screwed up syntax (don't know why), but there's no explanation what error exactly in the page.
Some posts with correct id's and usernames actually do show up with the script, on two of the three posts, but nothing at all on the third. So some result, but very messy and I can't call it a success.
See if this works:
<?php
// 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, username, rssfeed FROM members WHERE rssfeed != ''";
$result = mysql_query($query);
$feeds = array();
$id_array = array();
$first = array();
$user = array();
$userinfo = array();
while ($cur_feed = mysql_fetch_assoc($result))
{
$feeds[] = $cur_feed['rssfeed'];
$userinfo[$cur_feed['rssfeed']] = $cur_feed;
}
// 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];
$username = $user[$ix];
$ix++;
// 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");
$counter=0;
foreach($first_items as $item)
{
$feed = $item->get_feed();
$info = $userinfo[$item->feed->feed_url];
$counter++;
if($counter >= 10)
break;
if ($img_size = @getimagesize($pun_config['o_avatars_dir'].'/'.$id.'.jpg'))
$avatar_field = '<img src="'.$pun_config['o_avatars_dir'].'/'.$id.'.jpg" alt="" />';
else
$avatar_field = '<img src="'.$pun_config['o_avatars_dir'].'/nopicture.png" alt="" />';
$picture = '<div id="feedpicture"><a href="member.php?id='.$user_id.'" title="'.$firstname.'\'s Member Page">'.$avatar_field.'</a></div>';
// Begin the (X)HTML page.
?>
<h3><?php echo $picture; ?><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 -->
<? echo trim(substr((str_replace("\n", ' ', str_replace("\r", ' ', strip_tags($item->get_description())))),0,300)); ?>
... <i>more</i></a><br />
<p class="footnote"><a href="member.php?id=<?php echo $info['id']; ?>" title="<?php echo $info['firstname'].'\'s Member Page'; ?>"><?php echo $info['username']; ?></a> | <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
}
?>
See if this works:
...
It does!
I'll email you to settle payment.
Just had to insert $info['id'] here and there to get the member pictures (only one at the moment) to show up.
Finally ready to put it live!!
Elbekko, mind if I brainpick over that code?
Firstly, does this line:
$userinfo[$cur_feed['rssfeed']] = $cur_feed;
fetch the items from the db info, and then place them into an array of the likes of:
$userinfo[the feed url][id]
$userinfo[the feed url][firstname]
$userinfo[the feed url][username]
$userinfo[the feed url][rssfeed]
?
Secondly, this line:
$info = $userinfo[$item->feed->feed_url];
How exactly does that item->feed->feed_url bit work? Is that a key as stored in the first_items array, or other?
Just trying to broaden my grasp of the multi-dimensional arrays. These devils have always eluded me thus far.
Cheers,
Matt
The first bit you are correct about yes.
The second bit is merely accessing the SimplePie class, had to digg through the code a bit for that
Cheers. The arrays are always one area that I've found somewhat mystic so far. Their logic defies me at the moment to a degree.
Thanks again for the explanation.
PunBB Forums → Programming → RSS feeds from database + associated data (skip NULL in MySQL output)
Powered by PunBB, supported by Informer Technologies, Inc.