2012-05-25 88 views
-2

我只想在我的網站上顯示最新的5個Feed。
我使用下面的代碼來獲取RSS訂閱...可以將顯示任一組的幫助有限,飼料...謝謝你提前:)關於限制Rss Feed

代碼不正在使用

<?php 
require_once('rss_fetch.inc'); 

$url = 'http://news.google.com/news?ned=us&topic=h&output=rss'; 

$rss = fetch_rss($url); 

echo "Site: ", $rss->channel['title'], "<br>\n"; 
foreach ($rss->items as $item) { 
    $title = $item['title']; 
    $url = $item['link']; 
    $desc = $item['description']; 
    $category = $item['category']; 
    echo "<a href=$url>$title</a>$desc <br/>CATEGORY : $category <br/><br/> "; 
} 
?> 
+3

把'foreach'變成一個循環,使用一個計數器上升到5? – bdares

回答

2

限制它使用foreach

foreach ($rss->items as $i => $item) { // use $i as counter 
    $title = $item['title']; 
    $url = $item['link']; 
    $desc = $item['description']; 
    $category = $item['category']; 
    echo "<a href=$url>$title</a>$desc <br/>CATEGORY : $category <br/><br/> "; 
    if($i == 4) break; // add this, == 4 is because $i starts from 0 
} 
+0

它的工作謝謝你guyzzzzzzzzzzz ......... – Triveni

1

如果您希望限制帖子的數量,您只需要跟蹤它們並在適用的情況下襬脫foreach循環,例如

<?php 
require_once('rss_fetch.inc'); 

$url = 'http://news.google.com/news?ned=us&topic=h&output=rss'; 

$rss = fetch_rss($url); 

echo "Site: ", $rss->channel['title'], "<br>\n"; 
$numposts = 0; 
$maxposts = 5; 
foreach ($rss->items as $item) { 
    $numposts++; 
    if ($numposts<=$maxposts) { 
    $title = $item['title']; 
    $url = $item['link']; 
    $desc = $item['description']; 
    $category = $item['category']; 
    echo "<a href=$url>$title</a>$desc <br/>CATEGORY : $category <br/><br/> "; 
    } else { 
    break; 
    } 
} 
?> 
+0

其工作感謝你guyzzzzzzzzzzz ......... – Triveni