/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Help me limit RSS Feed

Is there any way I can modify the following code to only limit 5 items and then have a link where the user can click to see the remaining feeds from the rss??

Thanks,

Jeff

[FONT=Courier New]<?php
require_once ‘../magpierss/rss_fetch.inc’;

$url = ‘http://news.google.com/news?hl=en&ie=UTF-8&q=compliance&btnG=Search+News&output=rss‘ ;

$rss = fetch_rss($url);

echo ‘Site: ‘ , $rss->channel[‘title’], ‘ <br / >’;
if ( $rss and !$rss->ERROR) {
foreach ($rss->items as $item ) {
echo ‘ <p class=text><a href=”‘ . $item[link] . ‘”>’ . $item[title] . ‘ </a><br / >’;
echo ‘Publish Date: ‘ . $item[pubdate] . ‘ <br / >’;
echo $item[ description ] . ‘ </p>’ ;
}
} else {
echo ‘RSS Error: ‘ . $rss->ERROR . ‘ <br / ><br />’ ;
}
?>[/FONT]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 26.2006 — [code=php]
<?php
if ( $rss and !$rss->ERROR) {
$counter == 0; // use to track how many items we've displayed
foreach ($rss->items as $item ) {
if(++$counter >= 5)
{
echo "<p><a href='link_to_all_items'>See more...</a></p>n";
break; // break out of foreach loop
}
echo ' <p class=text><a href="' . $item[link] . '">' . $item[title] . ' </a><br / >';
echo 'Publish Date: ' . $item[pubdate] . ' <br / >';
echo $item[ description ] . ' </p>' ;
}
}
?>
[/code]
Copy linkTweet thisAlerts:
@jazzyj99authorApr 26.2006 — That's exactly what I needed?
Copy linkTweet thisAlerts:
@BrygeeAug 31.2014 — [code=php]
<?php
if ( $rss and !$rss->ERROR) {
$counter == 0; // use to track how many items we've displayed
foreach ($rss->items as $item ) {
if(++$counter >= 5)
{
echo "<p><a href='link_to_all_items'>See more...</a></p>n";
break; // break out of foreach loop
}
echo ' <p class=text><a href="' . $item[link] . '">' . $item[title] . ' </a><br / >';
echo 'Publish Date: ' . $item[pubdate] . ' <br / >';
echo $item[ description ] . ' </p>' ;
}
}
?>
[/code]
[/QUOTE]


If you are using the parser in numerous instances you can always incoroprate it into a function:

[CODE]<?php function SHOWRSS($url,$limit){
$rss = simplexml_load_file($url);
if($rss):
#######################################
$rsstitle = $rss->channel->title;
$rsspubDate = $rss->channel->pubDate;
echo '<h2>'.$rsstitle.'</h2><br>';
echo '<h3>'.$rsspubDate.'</h3><br>';
#######################################
$items = $rss->channel->item;
foreach($items as $item):
$counter == 0;
if(++$counter >= $limit+1){
break; }

#########################
$rssheadline = $item->title;
$rsslink = $item->link;
$rsspub = $item->pubDate;
$rssdesc = $item->description;
##########################
echo '<h3>'.$rssheadline.'</h3>';
echo '<span class="sm">('.$rsspub.')</span>';
echo '<p>'.$rssdesc.'</p>';
endforeach; endif;} ?>[/CODE]


then to serve you just need to add the following code:

<?php echo SHOWRSS('FEEDURL',NUMBERCOUNT); ?></div>

<?php echo SHOWRSS('http://open.live.bbc.co.uk/weather/feeds/en/2640101/3dayforecast.rss',2);?>

[CODE]<?php echo SHOWRSS('http://news.yahoo.com/rss/',8); ?></div>

<?php echo SHOWRSS('http://open.live.bbc.co.uk/weather/feeds/en/2640101/3dayforecast.rss',2);?> [/CODE]
Copy linkTweet thisAlerts:
@NogDogAug 31.2014 — Ooh...nice job of re-opening an 8-1/2 year old thread.
×

Success!

Help @jazzyj99 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...