william205
william205

Reputation: 688

Retrieve RSS Feed Data with for loop

Forgive me because my knowledge of PHP is limited but I have this code which retrieves all the items from an RSS Feed but I now need it to be using a for loop instead of a foreach loop so that I can limit the amount of times it runs and what item number it starts from. How would I go about doing this? Thank you for your help in advance.

$urls = array("WordlideVideo" => "http://feeds.reuters.com/reuters/USVideoWorldNews");

$rss = fetch_rss($urls[$_GET['url']]);

foreach ($rss->items as $item) {
    $href  = $item['link'];
    $title = $item['title'];
    $video = $item['video'];
    $titleLength = strlen($title);

if ($titleLength > 180) {
    $title = substr($title, 0, 177);
    $title = $title . "...";
} else { 
    $title = $title;
}

}

Upvotes: 0

Views: 1086

Answers (1)

Giles Williams
Giles Williams

Reputation: 679

Assuming $rss->items is an array, you should be able to do something like the following:

$items = $rss->items;
$limit = count($items);

// put any logic here to reduce $limit if it's greater than your threshold

for($i=0; $i<$limit; $i++) {
    $item = $items[$i];

    // code as before inside foreach loop
}

Upvotes: 1

Related Questions