AV1611 Posted July 20, 2009 Share Posted July 20, 2009 Here is a snippet I use to display an xml feed on another page. I need to know how to make it only show the first 5 items in the feed rather than all of them: <?php require_once "XML/RSS.php"; $file=file_get_contents('http://mypage.com/wordpress/?feed=rss2'); $r=& new XML_RSS($file); $r->parse(); //echo '<pre>'; //print_r($r); //echo '</pre>'; $i=0; foreach($r->getItems() as $entry) { echo '<div class="infoblock">'; echo "<p>"; echo '<a style="font-size: 130%;" href="'; echo $entry['link']; echo '">'; echo $entry['title']; echo "</a></p>"; $line=$entry['content:encoded']; $line=str_replace('/>','>',$line); $line=str_replace('</p>','</p><p> </p>',$line); echo $line; echo "<p style='text-align: center;'><a href='".$entry['comments']."'>Click here for comments or to leave a reply</a></p><p> </p>"; echo "</div>\n"; } ?> I know this is where the items come from foreach($r->getItems() as $entry) but I don't know how to make the foreach also do while $i < 5 or whatever... Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/166582-solved-limit-xml-feed/ Share on other sites More sharing options...
Alex Posted July 20, 2009 Share Posted July 20, 2009 You can use a while() loop, a for() loop, or just add an incremented number into your foreach() like.. $i = 0; foreach($r->getItems() as $entry) { if($i >= 5) break; echo '<div class="infoblock">'; echo "<p>"; echo '<a style="font-size: 130%;" href="'; echo $entry['link']; echo '">'; echo $entry['title']; echo "</a></p>"; $line=$entry['content:encoded']; $line=str_replace('/>','>',$line); $line=str_replace('</p>','</p><p> </p>',$line); echo $line; echo "<p style='text-align: center;'><a href='".$entry['comments']."'>Click here for comments or to leave a reply</a></p><p> </p>"; echo "</div>\n"; $i++; } Link to comment https://forums.phpfreaks.com/topic/166582-solved-limit-xml-feed/#findComment-878400 Share on other sites More sharing options...
AV1611 Posted July 20, 2009 Author Share Posted July 20, 2009 I was not able to figure out where to put the braces when I tried to loop it but you BREAK statement worked great... never did that before Thanks! Link to comment https://forums.phpfreaks.com/topic/166582-solved-limit-xml-feed/#findComment-878404 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.