Jump to content

Sorting upcoming and past events from repeater field


tron1000
 Share

Recommended Posts

Hello everyone

My question is very noobish, but I found no way to accomplish something simple. Also related posts in the forum where to difficult to work with for me …

I would like to sort upcoming and past events which are repeaters by their end date field.

The output should look something like this:

UPCOMING EVENTS

Title event 1

26. November to 27. November 2014

Title event 2

29. November  2014

PAST  EVENTS

Title event 3

12. November 2014

Title event 4

1. August to 5. August 2014

My code only returns the last of the created events as many times as there are events ???. Am I completely on the wrong way? Any hint would be very appreciated …

Thanks, tron1000

setlocale(LC_TIME, 'de_DE');

echo "<h1>Events</h1>";

foreach($page->event as $event) {

$event_title = $event->event_title;
$date_to = $event->getUnformatted('event_date_to');
$today = time();

$date_from_formatted = strftime('%d. %B %Y', $event->getUnformatted('event_date_from'));
$date_to_formatted = strftime('%d. %B %Y', $event->getUnformatted('event_date_to'));

if ($date_from_formatted == $date_to_formatted)
$event_out = "{$event_title}<br>{$date_from_formatted}<br><br>";

else
$event_out = "{$event_title}<br>{$date_from_formatted} to {$date_to_formatted}<br><br>";

}

echo "UPCOMING<br><br>";

if ($date_to > $today)

foreach($page->event as $event) {
echo $event_out;
}

echo "PAST<br><br>";

if ($date_to < $today)
foreach($page->event as $event) {
echo $event_out;
}
Link to comment
Share on other sites

You should do something like this. Of course it can be simplified and refactored, but still...

    echo "UPCOMING<br><br>";
     
    foreach($page->event as $event) {
	$event_title = $event->event_title;
	$date_to = $event->getUnformatted('event_date_to');
	$today = time();
		 
	$date_from_formatted = strftime('%d. %B %Y', $event->getUnformatted('event_date_from'));
	$date_to_formatted = strftime('%d. %B %Y', $event->getUnformatted('event_date_to'));
		 
	if ($date_from_formatted == $date_to_formatted)
	$event_out = "{$event_title}<br>{$date_from_formatted}<br><br>";
		 
	else
	$event_out = "{$event_title}<br>{$date_from_formatted} to {$date_to_formatted}<br><br>";
	
	if ($date_to > $today) echo $event_out;
    }

Same for past events.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...