Jump to content


Photo

Array limit


  • Please log in to reply
3 replies to this topic

#1 chris9902

chris9902
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationUK

Posted 09 October 2006 - 09:34 AM

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.

$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]);


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

#2 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 09 October 2006 - 09:55 AM

The cheap and nasty way would be as follows

$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]);


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).
SA PHP Archives - www.phparchives.za.org

#3 chris9902

chris9902
  • Members
  • PipPip
  • Member
  • 14 posts
  • LocationUK

Posted 09 October 2006 - 10:01 AM

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.

#4 xsist10

xsist10
  • Members
  • PipPipPip
  • Advanced Member
  • 114 posts

Posted 09 October 2006 - 10:15 AM

How about this

$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";
}


SA PHP Archives - www.phparchives.za.org




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users