Jump to content

Repeaters: sorting input fields; listing according to input fields


joe_ma
 Share

Recommended Posts

Hello

I am building a site for a festival, the events of which can take place on several dates and/or times and in several locations. Like this:

- event 1: 25. 6. 2014, 14:00 h, location 1

                25. 6. 2014, 16:00 h, location 2

                26. 6. 2014, 14:00 h, location 1

- event 2: 25. 6. 2014, 14:00 h, location 3

                26. 6. 2014, 14:00 h, location 2

and so on.

So I created a repeater field called "time_loc" with the input fields date (for the date and the time) and location.

Now I should like to output a list of events for each date, like this:

25. 6. 2014

- 14:00 h, event 1, location 1

- 14:00 h, event 2, location 3

- 16:00 h, event 1, location 2

26. 6. 2014

- 14:00 h, event 1, location 1

- 14:00 h, event 2, location 2

But I haven't the faintest about how to sort the date fields from the repeater field, nor how to make the list.

Thanks for help.

Link to comment
Share on other sites

Below is a code that sort of did a similar thing - you should be able to adapt it.

Actually it went further than you in that is breaks the date field down by first of all year, and then by month, and then by date and then by time

And then it stuck it all into a series of tables.

The output is here (sorry, they are a bit behind filling in fixtures...)

http://www.harlestonanglingclub.co.uk/fixture-list/

function events() {
// get all of the events
    $events = wire("page")->events; //gets the repeater field
    $years = array();
    
    $out ="";
    // find the array of years for all events
    foreach ($events as $event) {
        $years[]= date("Y", $event->getUnformatted("event_start_time"));
        // add properties event_year and event_month to the $event object
        $event->event_year = date("Y", $event->getUnformatted("event_start_time"));
        $event->event_month = date("m", $event->getUnformatted("event_start_time"));
    }

    $years = array_unique($years);
    asort($years);

// for testing
// print_r($years);
// print_r($events);

    foreach($years as $key => $year) {

        // Output the year
        $out .="<h2>{$year}</h2>";

        // find the array of events this year and put into new array $year_events
        $year_events = $events->find("event_year=$year");
         // print_r($year_events);



        // loop through the events for this year and add the months to the array
        $months = array();
        foreach ($year_events as $year_event) {
            $months[]= date("m", $year_event->getUnformatted("event_start_time"));
        }
        $months = array_unique($months);
        asort($months);
         // print_r($months);

        // loop through the months and find events for the month
        foreach($months as $key => $month) {
            
            // Output the month as a number
            $monthName = date("F", mktime(0, 0, 0, $month, 10));
$out .="<h3>{$monthName}, {$year}</h3>";

            // filter only the events for this month, from the $year_events array.
            $month_events = $year_events->find("event_month=$month");
             // print_r($month_events);
$out .="<div class='table-responsive'>";
$out .="<table class='table table-bordered table-responsive'>";
$out .="<thead>";
$out .="<tr class='success'>";
$out .="<th>Date</th>";
$out .="<th>Who</th>";
$out .="<th>Place & Event</th>";
$out .="<th>Time</th>";
$out .="</tr>";
$out .="</thead>";
$out .="<tbody>";

                foreach($month_events as $e) {

$out .="<tr>";
$start_date = date("l jS", $e->getUnformatted("event_start_time"));
$out .="<td><strong>{$start_date}</strong></td>";
$out .="<td>{$e->event_who}</td>";
$out .="<td>{$e->event_where}</td>";

$start_time = date("g:s a", $e->getUnformatted("event_start_time"));
$end_time = date("g:s a", $e->getUnformatted("event_end_time"));

$out .="<td>{$start_time} to {$end_time}</td>";

$out .="</tr>";

                } // end foreach events for this month

                $out .="</tbody></table></div>";

        } // end foreach months
    
    } // end foreach years

echo $out;
}
Link to comment
Share on other sites

OK, back in the office.

I have tried to modify Joss’s code for my needs. Alas, no success!

So here’s my setup:

- every event is on its own page as a subpage of /events/

- every event page has a repeater field called "time_loc" with the fields "date" and "location"

And here’s my code:

    $events = $pages->find("parent=/events/"); //find all pages with events

    $datum = $events->time_loc; //find the repeater field in each page
    
    $days = array();
    
    $out ="";
    // find the array of dates for all events
    foreach ($datum as $d) {
        $days[]= $d->date;
        //add d_datum property
        $d->d_datum = date("l d F Y", $d->getUnformatted("date"));
        //add d_time property
        $d->d_time = date("H:i", $d->getUnformatted("date"));
    }

 
 
    $days = array_unique($days);
    asort($days);

    foreach($days as $key => $day) {

        // Output the date
        $out .="<h2>{$day}</h2>";

        // find the array of events on this date and put into new array $day_events
        $day_events = $d->find("d_datum=$days");
        
        $out .= "<ul>";

        foreach ($day_events as $day_event){
        
            $out .= "<li>{$day_event->title}<br>{$day_event->d_time} Uhr; {$day_event->time_loc('location')}</li>";
            
        }
        $out .="</ul>";
    }

echo $out;


That doesn’t output anything.

Link to comment
Share on other sites

  • 3 weeks later...

Well, back from my holydays. I thought, a little timeout would do some good to my brain. But alas …

I am really stuck on this issue and would appreciate any help or hints about the right direction to go. I think I went wrong right at the beginning with the $days array, but I haven't a clue about how.

Link to comment
Share on other sites

Nothing at all. No HTML Markup either.

As far as I could find out with testing, this code is finding the pages with the events. I could produce a list of all the pages with events.

But I could not find out, how to handle the dates and times of the events correctly.

There are events, that can take place on more than one day, or twice or more times at the same day and even on different locations. All this is stored in the repeater field.

What I try to achieve is a list like that:

Date 1:

- event 1, time xx, location yy

- event 1, time xy, location yz

- event 2, time, location

Date 2:

-event 1, time, location

-event 2, time, location

and so on, you get the idea.

Oh, silly me. I think I misunderstood your question. The HTML structure is being output, but no lists whatsoever.

So I have tried to work slowly from the beginning like this:

    $programm = $pages->find("template=event"); //find all pages of events that contain the repeater field

So far everything is ok, I could output a list of these pages. Next step:

    $events = $programm->time_loc; //find the repeater fields that include the fields "date" and "location"

    $days = array();
    
     // find the array of dates for all events
    foreach ($events as $event) {
        $days[]= date("l, d. M Y", $event->getUnformatted("date"));
    }
        
    $days = array_unique($days);
    asort($days);

print_r ($days);

This outputs

Array()

So, clearly there must be something wrong already here. But I have still no clue.

Edited by joe_ma
Link to comment
Share on other sites

Ah, thanks Adrian. This brought me one step further. My code now looks like this (for testing the dates):

	$programm = $pages->find("template=event"); //find all pages with events
	
	foreach ($programm as $p) {
		
		$events = $p->time_loc;	//find the repeater fields
		
	    $days = array();  // array for dates
    
			foreach ($events as $event) {

		        $days[]= date("l, d. M Y",$event->getUnformatted("date")); //fill the dates array with the dates from the repeater
		    }
	

	    $day = array_unique($days); // eliminate duplicate dates
	    asort($day);

		    foreach($day as $key => $d) {

				echo "<p>$key = $d</p>";
			}

	}

That outputs this:

0 = Thursday, 10. Apr 2014

1 = Tuesday, 15. Apr 2014

2 = Friday, 25. Apr 2014

1 = Thursday, 24. Apr 2014

0 = Wednesday, 23. Apr 2014

1 = Thursday, 15. May 2014

0 = Wednesday, 07. May 2014

0 = Saturday, 26. Apr 2014

0 = Sunday, 27. Apr 2014

0 = Monday, 28. Apr 2014

0 = Monday, 28. Apr 2014

So, clearly I still haven't got the array_unique right.

Link to comment
Share on other sites

The clue is in the array keys - see how there are lots of the same numbers? Your $days = array(); is inside the $programm loop so it is getting reset each time. 

Also you are outputting the results inside the $programm loop.

Try this. Untested, but I think it should be right.

    $programm = $pages->find("template=event"); //find all pages with events
    
    $days = array();  // array for dates - defined once at the start
    foreach ($programm as $p) {
        
        $events = $p->time_loc; //find the repeater fields
          
        foreach ($events as $event) {
            $days[]= date("l, d. M Y",$event->getUnformatted("date")); //fill the dates array with the dates from the repeater
        }
    } //close programm loop

    $day = array_unique($days); // eliminate duplicate dates
    asort($day);

    foreach($day as $key => $d) {
        echo "<p>$key = $d</p>";
    }
  • Like 1
Link to comment
Share on other sites

Well, nearly there …

This is my code:

    $programm = $pages->find("template=event"); //find all pages with events
    
    $days = array();  // array for dates - defined once at the start

    foreach ($programm as $p) {
        
        $events = $p->time_loc; //find the repeater fields
          
        foreach ($events as $event) {
 
			$days[] = $event->getUnformatted("date");
			$event->event_title = $p->title; // add propertiy event_title to the $event object
			$event->event_url = $p->url; // add propertiy event_url to the $event object
        }
		
    } //close programm loop

    $day = array_unique($days); // eliminate duplicate dates
    asort($day);

    foreach($day as $key => $d) { // list all the dates

        $dat_events = $events->find("date=$d");  // find the array of events for this date and put into new array $dat_events
	$dat = date('l, d. M Y', $d);

	echo "<h3>($key) = $dat</h3>";
      
		foreach ($dat_events as $dat_ev){ //loop through the dayly events and link to them
				
			echo "<p><a href='{$dat_ev->event_url}'>{$dat_ev->event_title}, {$dat_ev->time} Uhr</a></p>";

		} // close loop for dayly events
		
    } //close loop for list of dates

That outputs this:

(0) = Thursday, 10. Apr 2014
(1) = Tuesday, 15. Apr 2014
(2) = Wednesday, 23. Apr 2014
(3) = Thursday, 24. Apr 2014
(4) = Friday, 25. Apr 2014
(7) = Saturday, 26. Apr 2014
(8) = Sunday, 27. Apr 2014
(9) = Monday, 28. Apr 2014

ZWISCHEN REBELLION UND RESIGNATION, 21:00 Uhr

(5) = Wednesday, 07. May 2014
(6) = Thursday, 15. May 2014

So only the events for the last key in the array of $day are listed.

Still confused.

Link to comment
Share on other sites

Macrura kindly helped me with another, similar problem with repeater fields. Thanks again.

So I tried to simplify the code for the problem with the listing of the daily events.

    $days = array();  // array for dates
    
    $events = $pages->find("template=repeater_time_loc, check_access=0"); //get all the repeaters with event information

        foreach ($events as $event) {
 
            $days[] = $event->getUnformatted("date"); // put event date into the array
            $vorstellung = $event->getForPage(); // get the parent page of the repeater
            
        }
    
    $day = array_unique($days); // eliminate duplicate dates
    asort($day);

    foreach($day as $key => $d) { // list all the dates

        $today_events = $events->find("date=$d");  // find the array of events for this date and put into new array $today_events
        $dat = date('l, d. M Y', $d); 

        echo "<h3>($key) = $dat</h3>";
        
        foreach ($today_events as $doday_ev) {
            
                echo "<p><a href='{$vorstellung->url}'>{$vorstellung->title}, {$today_ev->time} Uhr</a></p>";
        }
    }

But that still doesn't work correctly. Now I get the list of the dates correctly displayed, but each date lists only one event and this is always the same one (Note the exception for Monday, 28. Apr 2014).

(0) = Thursday, 10. Apr 2014
ZWISCHEN REBELLION UND RESIGNATION, Uhr

(1) = Tuesday, 15. Apr 2014
ZWISCHEN REBELLION UND RESIGNATION, Uhr

(2) = Wednesday, 23. Apr 2014
ZWISCHEN REBELLION UND RESIGNATION, Uhr

(3) = Thursday, 24. Apr 2014
ZWISCHEN REBELLION UND RESIGNATION, Uhr

(4) = Friday, 25. Apr 2014
ZWISCHEN REBELLION UND RESIGNATION, Uhr

(7) = Saturday, 26. Apr 2014
ZWISCHEN REBELLION UND RESIGNATION, Uhr

(8) = Sunday, 27. Apr 2014
ZWISCHEN REBELLION UND RESIGNATION, Uhr

(9) = Monday, 28. Apr 2014
ZWISCHEN REBELLION UND RESIGNATION, Uhr
ZWISCHEN REBELLION UND RESIGNATION, Uhr

(5) = Wednesday, 07. May 2014
ZWISCHEN REBELLION UND RESIGNATION, Uhr

(6) = Thursday, 15. May 2014
ZWISCHEN REBELLION UND RESIGNATION, Uhr

Link to comment
Share on other sites

joe_ma. I like your tenacity! Sorry to see you are still struggling with this.

Not a direct answer to your query, but I would like to suggest debugging your code 1 step at a time can help you find where the error is coming from; it is also fun and will make you a better coder in the long run. That's what I learnt from the gurus here :-)...

You could do something like this:

$events = $pages->find("template=repeater_time_loc, check_access=0"); //get all the repeaters with event information

echo gettype($events) . '<br>';//this will tell you the type of variable you are dealing with [just an example]

//if it is an object/array...
echo '<pre>';
echo print_r($events);//verify $events is returning what you were expecting
echo '</pre>';
exit;//stop other code executing

Keep on doing this or similar line by line, Check even inside the foreach (e.g. print_r($days)) to verify things are being returned as expected, exiting after short lines of code, until the next variable, etc, etc.. Were you expecting an integer? Was a string returned instead? etc...This way, you will be able to (hopefully) identify where things are going wrong. Make sure debug is turned on as well..

Anyway, just thought to share my experience....

Link to comment
Share on other sites

your code is kind of messed up, you can't set the variable when you're making the days array, that array has no relation to the later array

 $days = array();  // array for dates
    
    $events = $pages->find("template=repeater_time_loc, check_access=0"); //get all the repeaters with event information

        foreach ($events as $event) {
            $days[] = $event->getUnformatted("date"); // put event date into the array       
        }
    
    $day = array_unique($days); // eliminate duplicate dates
    asort($day);

    foreach($day as $key => $d) { // list all the dates

        $today_events = $events->find("date=$d");  // find the array of events for this date and put into new array $today_events
        $dat = date('l, d. M Y', $d);

        echo "<h3>($key) = $dat</h3>";
        
        foreach ($today_events as $doday_ev) {
            
                echo "<p><a href='{$doday_ev->getForPage()->url}'>{$doday_ev->getForPage()->title}, {$today_ev->time} Uhr</a></p>";
        }
    }
  • Like 2
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...