This is a little more complicated. You have to loop through the elements first, and create a twodimensional array containing weekday and time.
$entries = array();
while ( have_rows('tag') ) : the_row();
//clean thisentry
$thisentry = array();
// set aufguss and sauno for thisentry
$thisentry['aufguss'] = get_sub_field('aufguss');
$thisentry['sauna'] = get_sub_field('sauna');
// populate array for wochentag/uhrzeit
$entries[get_sub_field('wochentag')][get_sub_field('uhrzeit')] = $thisentry;
endwhile;
Now you can define your Weekdays and times, like:
$wochentage = array( 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'freitag', 'Samstag', 'Sonntag' );
$uhrzeiten = array( '11:00', '11:30', '12:00', '12:30' );
And start building up the table, making a nested loop through times and days. I loop through the times first, so I can build the lines of my table.
For example, my time has the value 11:00
, and I loop through all the days, and add the entry of each day for the time 11:00
to a new line of the table.
For each time I create an additional cell with the time itself, and for each day I add another cell to my first line, so I get the complete table, and add this together to create a full table. Echo it and voila 🙂
The whole commented code:
$entries = array();
while ( have_rows('tag') ) : the_row();
//clean thisentry
$thisentry = array();
// set aufguss and sauno for thisentry
$thisentry['aufguss'] = get_sub_field('aufguss');
$thisentry['sauna'] = get_sub_field('sauna');
// populate array for wochentag/uhrzeit
$entries[get_sub_field('wochentag')][get_sub_field('uhrzeit')] = $thisentry;
endwhile;
$wochentage = array( 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'freitag', 'Samstag', 'Sonntag' );
$uhrzeiten = array( '11:00', '11:30', '12:00', '12:30' );
//create first cell of heading line
$thead.= '<tr><td> </td>';
// loop through each line of the output
foreach( $uhrzeiten as $uhrzeit ) {
// create new line with first cell of time
$lines.= '<tr><td>' . $uhrzeit . '</td>';
// loop through each day
foreach( $wochentage as $wochentag ) {
//create day cell of heading line
$thead.= '<td>' . $wochentag . '</td>';
//create the elements
$aufguss = "";
$sauna = "";
if( $entries[$wochentag][$uhrzeit]['aufguss'] ) {
$thisaufguss = get_post( $entries[$wochentag][$uhrzeit]['aufguss'] );
$aufguss="<a href="" . get_permalink( $thisaufguss->ID ) . '">' . $thisaufguss->post_title . '</a><br />';
}
if( $entries[$wochentag][$uhrzeit]['sauna'] ) {
$thissauna = get_post( $entries[$wochentag][$uhrzeit]['sauna'] );
$sauna="<a href="" . get_permalink( $thissauna->ID ) . '">' . $thissauna->post_title . '</a><br />';
}
// add the entry to the lines
$lines.= '<td>' . $aufguss . $sauna . '</td>';
}
//close the line
$lines.= '</tr>';
}
//close heading line
$thead.= '</tr>';
//create the table
$table="<table>" . $thead . $lines . '</table>';
echo $table;