Jump to:

23552 Posts in 19349 Topics by 2891 members

General Questions

SilverStripe Forums » General Questions » Reversing RSS Feed/ DataObjectSet

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

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

Page: 1
Go to End
Author Topic: 1018 Views
  • Tama
    Avatar
    Community Member
    130 Posts

    Reversing RSS Feed/ DataObjectSet Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5511 Posts

    Re: Reversing RSS Feed/ DataObjectSet Link to this post

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

  • Tama
    Avatar
    Community Member
    130 Posts

    Re: Reversing RSS Feed/ DataObjectSet Link to this post

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

  • davidm2010
    Avatar
    Community Member
    107 Posts

    Re: Reversing RSS Feed/ DataObjectSet Link to this post

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

  • Tama
    Avatar
    Community Member
    130 Posts

    Re: Reversing RSS Feed/ DataObjectSet Link to this post

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

    1018 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.