Jump to content

Archived

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

chris9902

Array limit

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 :)

Share this post


Link to post
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.