0
<?php
    $val = $_GET["val"];
    $url = "http://feeds.bbci.co.uk/news/rss.xml";
    $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;
        $rss .= "<a href='$link'><h3>$title</h3></a>";
        $rss .= "$description";
        $rss .= "<br />$pubDate<hr />";
   }
   echo $rss;
?>

Hello everyone! I have a problem here. I would like to list all the results for the rss link but it gets only 10. I know that I have the second condition for "for loop" as $i<10, but how can I remove that condition, and get all the results from the rss link?

Suneel Kumar
  • 1,650
  • 2
  • 21
  • 31

2 Answers2

5

Use foreach instead of for:

<?php
$url = "http://feeds.bbci.co.uk/news/rss.xml";
$xml = simplexml_load_file($url);
$rss = '';

foreach ($xml->channel->item as $item) {

    $title = $item->title;
    $link = $item->link;
    $description = $item->description;
    $pubDate = $item->pubDate;

    $rss .= "<a href='$link'><h3>$title</h3></a>";
    $rss .= "$description";
    $rss .= "<br />$pubDate<hr />";
}
echo $rss;
?>
Jirka Hrazdil
  • 3,983
  • 1
  • 14
  • 17
1

Either you can use foreach loop or count the size of an array and then use this size to set the second condition in your for loop..

Akshit Ahuja
  • 337
  • 2
  • 15