Question

how to make My rss reader display 10 feed then on clicking next display another 10 and so on my code is :

<?php 
$html = ""; 
$url = "http://rss.news.yahoo.com/rss/topstorie... 
$xml = simplexml_load_file($url); 
for($i = 0; $i < 10; $i++){ 
$title = $xml->channel->item[$i]->title; 
$link = $xml->channel->item[$i]->link; 
$description = $xml->channel->item[$i]->description; 
$pubDate = $xml->channel->item[$i]->pubDate; 

$html .= "<a href='$link'><h3>$title</h3></a>"; 
$html .= "$description"; 
$html .= " 
$pubDate<hr />"; 
} 
echo $html;
?> 

when is increase for($i = 0; $i < 10; $i++) this then the results also get increased but are show in a same page i want to know how to make it show only 10 feeds then when user click net the next 10 are shown and previous 10 are hidden

Was it helpful?

Solution

One easy way to do that is to paginate with LimitIterator over a SimpleXMLIterator specifying the page number and the size of each page:

$url = 'http://news.yahoo.com/rss/world/';
$rss = simplexml_load_file($url, 'SimpleXMLIterator');

$page = 2;
$size = 10;

$items = new LimitIterator($rss->channel->item, ($page - 1) * $size, $size);

printf("Page #%d:\n", $page);
foreach ($items as $item) {
    echo ' * ', $item->title, "\n";
}

Exemplary page 2 from right now:

Page #2:
 * Air raids on rebel areas near Damascus, Kurds advance: NGO
 * Author Yasmina Khadra to run for Algerian president
 * Fugitive eco-activist says granted Australian visa
 * Egypt family feud kills 10: police
 * French say 2 journalists killed in north Mali
 * Burnley held as Leicester close on leaders
 * Dundee United denied famous win as Celtic snatch point
 * Fire breaks out in Saudi prison, riots and gunshots reported
 * RFI: 2 French journalists kidnapped in north Mali
 * Los Angeles airport partly closed as shooting probe continues

This is plain-text only output, but I assume you can see in the example how it works and that it is easy to adopt for HTML output.

OTHER TIPS

You need a $page variable to store and send via link information about current page number. then you can loop like this.

$page = 1; $itemsPerPage = 10;

$start = ($page-1)*itemsPerPage ; $max = $page * $itemsPerPage ;

for($i = start ; $i < max; $i++)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top