Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Reversing RSS Feed/ DataObjectSet


Go to End


5 Posts   1547 Views

Avatar
Tama

Community Member, 138 Posts

15 June 2010 at 10:41am

Edited: 15/06/2010 10:42am

Morning

I've put together some code to pull an RSS list of events through so we can display them on our website. I have two issues:

1.) The events are sorted by descending date for the RSS field which means the most far away events are displayed at the top of the list.
2.) There are 74 events and I only want to show 5.

So I want to display the next 5 upcoming events. Here is my code:

Page.php
class Page_Controller extends ContentController {
	function RestfulLinks($url, $delimiter, $interval){

		$rssfeed = new RestfulService($url,$interval);
		$conn = $rssfeed->request('')->getBody();
		$result = $rssfeed->getValues($conn, $delimiter);

		return $result;
	}
}

HomePage.php
class HomePage_Controller extends Page_Controller {
	function EventList($num=5) {
		
		$RSSFeed = RestfulLinks("http://itson.co.nz/feeds/all/", "entry", 3600);
		$EventList = new DataObjectSet();
		$num_items=0;
		
		foreach($RSSFeed as $EventItem){
						
			$extrainfo = array(
				'EventTitle' => $EventItem->title,
				);
			$EventList -> push(new ArrayData($extrainfo));
			
			if (++$num_items == $num) break;
		}
		
		return $EventList;
	}
}

This returns the following:

* South Island U15 & U17 Badminton Championships (Monday 12th–Saturday 17th July 2010)
* Nelson Winter Music Festival - The Funky Monkeys (Monday 12th July 2010)
* 'The Ballad of Robin Hood'- Children's Theatre (Monday 12th–Tuesday 13th July 2010)
* Nelson Winter Music Festival - Josef Spacek & Michael Houstoun (Sunday 11th July 2010)
* Nelson Winter Music Festival - Kemp English (Sunday 11th July 2010)

What I'd like to return is the following:

* Nelson Farmers' Market (Wednesday 20th January 2010–Monday 31st January 2011)
* Shelter from the Storm (Saturday 24th April–Wednesday 30th June 2010)
* 'Mille-Fleurs' Art Exhibition at The Suter Art Gallery (Saturday 8th May–Sunday 20th June 2010)
* YMCA (Thursday 13th May–Saturday 31st July 2010)
* TREATY2U (Friday 14th May–Sunday 1st August 2010)

Does anyone have a good idea on how to achieve this? I can think of some pretty ugly methods but I'm hoping that the order of $RSSFeed can be easily reversed.

Thanks in advance
Tama

Avatar
Willr

Forum Moderator, 5523 Posts

15 June 2010 at 11:16am

Have you tried using the sort() method on the set - http://api.silverstripe.org/2.4/sapphire/model/DataObjectSet.html#methodsort

Avatar
Tama

Community Member, 138 Posts

23 June 2010 at 10:40am

Hi Will, thanks for that - I didn't notice the sort method. Will give it a crack.

Avatar
davidm2010

Community Member, 107 Posts

14 July 2010 at 10:58pm

I would like to know the outcome of this. Did the sort method work for you?

Avatar
Tama

Community Member, 138 Posts

15 July 2010 at 9:12am

Hi David - I replaced "return $EventList" with the following code:

		// Reverse order so near events show first
		$EventList -> sort("EndDate", "ASC");
		
		// Limit the number of events returned
		return $EventList -> getRange(0, $num );