0

如何让我的 rss 阅读器显示 10 个提要,然后单击下一个显示另一个 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;
?> 

何时增加 for($i = 0; $i < 10; $i++) 那么结果也会增加但显示在同一页面中我想知道如何使其仅显示 10 个提要然后当用户单击 net显示下 10 个,隐藏前 10 个

4

2 回答 2

0

一种简单的方法是LimitIterator通过SimpleXMLIterator指定页码和每页的大小进行分页:

$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 输出。

于 2013-11-02T20:07:32.330 回答
0

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

$页 = 1; $itemsPerPage = 10;

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

for($i = 开始;$i < 最大值;$i++)

于 2013-10-31T20:29:25.770 回答