这有点复杂。您必须首先循环遍历元素,并创建一个包含工作日和时间的二维数组。
$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\' );
然后开始构建表,在时间和日期中进行嵌套循环。我首先循环遍历times,这样就可以构建表的行。
例如,我的时间有价值11:00
, 我循环遍历所有的日子,并为时间添加每天的条目11:00
到表格的新行。
每次我用时间本身创建一个额外的单元格,每天我在第一行中添加另一个单元格,这样我就得到了完整的表,并将其添加到一起以创建一个完整的表。回音,瞧:)
整个注释代码:
$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;