How can I reverse the order of a foreach loop?

Aug 12, 2011 at 5:43 PM

This is my first experience with Graffiti.

I have been asked to make a change to a new client's existing site.

The goal is to list the Past Events in date DESC order, while keeping the Upcoming Events in date ASC order.

The site seems to have been set up (by the original developer) so that Events is a single category, set as date ASC sort order. The Upcoming Events page uses a foreach loop to print out this list, filtering out events with dates that have passed.

The Past Events page uses the SAME foreach loop to print out the SAME list, but this time filtering out events with dates that have not yet passed.

The problem is that then BOTH lists are sorted as date ASC. I want to reverse the order of the Past Events foreach loop so that the event that happened last week appears at the top, and events from 2009 appear at the bottom.

I've been searching and reading through this documentation, and can't find anything.

Here is the foreach loop in question:

#foreach($post in $data.PostsByCategory("events", 9999))
    #if($meadows.CheckforExpired($post.Title))
        ## show expired events
        <li class="event"><span class="date"><h4>$post.Custom("Event Date")</h4></span>$post.Body</li>
    #else
        ## show upcoming events (in the upcoming events list the if above is empty and this else has the <li>)
    #end
#end

I appreciate any help...

Editor
Aug 12, 2011 at 6:21 PM

thisisrobyn,

It's been a while since I've reviewed the GCMS source code, but I don't believe there is a way to do this in the current builds (I could be wrong, in which case the best option may be to upgrade to the latest version). 

If however, you don't want to try an upgrade to fix this minor issue, then I see two options: 1) Compile new build (same version that you have currently) and add a sort parameter on the PostsByCategory() function.  This would be very easy to do. 2) Use this extension http://chalkextensions.codeplex.com/ which has functions that will allow you to get a category with sorted posts.  If you use the extension you need to make sure it is compiled with the correct version of GCMS otherwise it's going to blow up on you.

Regards,
Brian 

Coordinator
Aug 13, 2011 at 3:02 PM
Edited Aug 13, 2011 at 3:04 PM

I've been using Graffiti Extensions to do this. It can change the sort order of any PostCollection. http://chalkextensions.codeplex.com/

It can also grab all posts from a category without having to use that 9999 trick.

Coordinator
Aug 14, 2011 at 4:44 AM

Just reversing the order isn't going to help you actually, since you want to have both lists (upcoming and past events) displayed. You'd have to go through the list twice to do what you want. Also, if you use the PostsByCategory function, it is going to return posts in the order that they were created, not the custom "Event Date". If your events are created in the order that they take place, this will work fine, otherwise they won't.

Not sure if this is using my Graffiti Events plugin or not, but if it is, I just updated the code on codeplex for just getting upcoming events or past events (and the ability to get all instead of X). Even if you aren't using that plugin, you can use the code as a baseline to write your own function(s) to get the events in the order that you want. Based on your custom field and category names, even if it isn't using the plugin right now, you should actually be able to just add it in and use the chalk extensions at the very least, because the names match what the plugin uses as defaults exactly.

Sep 26, 2011 at 11:27 PM

Have you tried using "Query(IDictionary parameters)" There is not too much documentation but it looks like you can select any order you want.

http://graffiticms.codeplex.com/wikipage?title=Data%20Extension%20Commands&referringTitle=Advanced%20Options