Jump to content

Array limit


chris9902

Recommended Posts

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 :)
Link to comment
https://forums.phpfreaks.com/topic/23406-array-limit/
Share on other sites

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).
Link to comment
https://forums.phpfreaks.com/topic/23406-array-limit/#findComment-106165
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/23406-array-limit/#findComment-106170
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.