chris9902 Posted October 9, 2006 Share Posted October 9, 2006 Hi all. It's monday and my brain no wants to work good!Making a very basic RSS reader and I want to limit the amount of data in an Array to say 10 stories.here is the code so far.[code]$xml[] = 'http://digg.com/rss/index.xml';$xml[] = 'http://news.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml';function get_RSS($url){ $RSS = simplexml_load_file($url); echo '<h1><a href="' . $RSS->channel->link . '">' . $RSS->channel->title . '</a></h1>'; foreach ($RSS->channel->item as $article) { echo '<ul>' . "\n"; echo ' <li class="title"><a href="' . $article->link . '">' . $article->title . '</a></li>' . "\n"; echo ' <li class="description">' . $article->description . '</li>' . "\n"; echo ' <li class="pubDate">' . $article->pubDate . '</li>' . "\n"; echo '</ul>' . "\n\n"; } }get_RSS($xml[0]);[/code]so right now it takes digg's feed and prints all the headlines but i want to limit it to 10. How can I do that?cheers :) Quote Link to comment Share on other sites More sharing options...
xsist10 Posted October 9, 2006 Share Posted October 9, 2006 The cheap and nasty way would be as follows[code]$xml[] = 'http://digg.com/rss/index.xml';$xml[] = 'http://news.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml';function get_RSS($url, $limit = 10){ $RSS = simplexml_load_file($url); echo '<h1><a href="' . $RSS->channel->link . '">' . $RSS->channel->title . '</a></h1>'; $current = 0; while ($current < $limit) { foreach ($RSS->channel->item as $article) { echo '<ul>' . "\n"; echo ' <li class="title"><a href="' . $article->link . '">' . $article->title . '</a></li>' . "\n"; echo ' <li class="description">' . $article->description . '</li>' . "\n"; echo ' <li class="pubDate">' . $article->pubDate . '</li>' . "\n"; echo '</ul>' . "\n\n"; $current++; } }}get_RSS($xml[0]);[/code]However if you want 10 headlines in total (from all your feeds) you should change the get_RSS function to return an array instead of printing the results out and then use that array to generate your display (you can limit the array, sort it, etc). Quote Link to comment Share on other sites More sharing options...
chris9902 Posted October 9, 2006 Author Share Posted October 9, 2006 thank for the reply. I had something like this already but as you say it's a bit nasty.I was hoping there was a way to just trim the Array down to 10 or something. I can't seem to find anything in the manual about it. Quote Link to comment Share on other sites More sharing options...
xsist10 Posted October 9, 2006 Share Posted October 9, 2006 How about this[code]$xml[] = 'http://digg.com/rss/index.xml';$xml[] = 'http://news.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml';function get_RSS($url){ $RSS = simplexml_load_file($url); $channel["link"] = $RSS->channel->link; $channel["title"] = $RSS->channel->title; $index = 0; foreach ($RSS->channel->item as $article) { $channel["article"][$index]["link"] = $article->link; $channel["article"][$index]["title"] = $article->title; $channel["article"][$index]["description"] = $article->description; $channel["article"][$index]["pubDate"] = $article->pubDate; $index++; } return $channel;}$feed = get_RSS($xml[0]);echo '<h1><a href="' . $feed["link"] . '">' . $feed["title"] . '</a></h1>';for ($i=0; $i<10; $i++) { echo '<ul>' . "\n"; echo ' <li class="title"><a href="' . $feed["article"][$i]["link"] . '">' . $feed["article"][$i]["title"] . '</a></li>' . "\n"; echo ' <li class="description">' . $feed["article"][$i]["description"] . '</li>' . "\n"; echo ' <li class="pubDate">' . $feed["article"][$i]["pubDate"] . '</li>' . "\n"; echo '</ul>' . "\n\n";}[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.