如何使我的RSS阅读器显示10个Feed然后单击下一个显示另一个10,依此类推

时间:2013-10-31 18:30:14

标签: php xml rss feed rss-reader

如何让我的RSS阅读器显示10个Feed然后单击下一个显示另一个10等等 我的代码是:

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

当($ i = 0; $ i&lt; 10; $ i ++)增加时,结果也会增加但显示在同一页面我想知道如何让它只显示10个Feed然后当用户点击网络显示下一个10,隐藏前10个

2 个答案:

答案 0 :(得分:0)

您需要$ page变量来存储和发送有关当前页码的链接信息。 然后你可以像这样循环。

  

$ page = 1;   $ itemsPerPage = 10;

     

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

     

for($ i = start; $ i&lt; max; $ i ++)

答案 1 :(得分:0)

一种简单的方法是使用LimitIteratorSimpleXMLIterator进行分页,指定页码和每页的大小:

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

现在的示例性第2页:

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

这是纯文本输出,但我假设您可以在示例中看到它是如何工作的,并且很容易采用HTML输出。