Log in

View Full Version : limit xml result



william james
01-25-2011, 01:29 AM
Hello Good People,

Please help me limit the entries generated on my site from a rss feed...
The code below shows all the entries.

Thankyou in advance!


<?php
$url = "http://news.google.com/?output=rss";
$rss = simplexml_load_file($url);
if($rss)
{
$items = $rss->channel->item;
foreach($items as $item)
{
$title = $item->title;
$link = $item->link;
$description = $item->description;
$image = $item->image;
echo ' <div class="img rand" >
<img src="'.$image.'" alt="'.$title.'">
<a href="'.$link.'" target="_blank">
<img src="/overlay.gif" class="cvr" alt="'.$title.'"/></a>
<div class="desc truncate" >'.$title.'</div>
</div>';
}
}
?>

Schmoopy
01-25-2011, 01:40 AM
Limit the results to how many?

You can use a simple counter:



<?php
$url = "http://news.google.com/?output=rss";
$rss = simplexml_load_file($url);
$limit = 10; // Change this number to however many items you want to display
$counter = 1;
if($rss)
{
$items = $rss->channel->item;
foreach($items as $item)
{
if($counter <= $limit) {
$title = $item->title;
$link = $item->link;
$description = $item->description;
$image = $item->image;
echo ' <div class="img rand" >
<img src="'.$image.'" alt="'.$title.'">
<a href="'.$link.'" target="_blank">
<img src="/overlay.gif" class="cvr" alt="'.$title.'"/></a>
<div class="desc truncate" >'.$title.'</div>
</div>';
$counter++;
}
}
}
?>

william james
01-25-2011, 02:20 AM
Thank You Sir for your help! No doubt it works and I know this thread will be helpful to others as well! :)