1

I want to achieve a schedule plan in my front-end. I cannot view information based on the Day. For example in the backend, a row is for Tuesday but its rendering on the column of monday in the front-end. I want it as, so entry for every day goes to its corresponding column and corresponding time. Please see the snapshots and my code below.

Front-End Front-End

Back-End Back-End

<div class="table-responsive">
 <table class="table table-bordered">
  <thead>
      <tr>
          <td></td>
          <td>MONTAG</td>
          <td>DIENSTAG</td>
          <td>MITTWOCH</td>
          <td>DONNERSTAG</td>
          <td>FREITAG</td>
          <td>SAMSTAG</td>
          <td>SONNTAG</td>
      </tr>
  </thead>



  <?php while(have_rows('tag')) :the_row();  ?>
    <tr>
        <td><?php echo get_sub_field('uhrzeit'); ?></td>   
        <td>
              <?php $aufguss_object = get_sub_field('aufguss');?>
              <?php $sauna_object = get_sub_field('sauna');?>

              <?php if($aufguss_object) { ?>
                  <?php $post = $aufguss_object; ?>
                  <?php setup_postdata( $post );  ?>
                  <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></br>
                  <?php wp_reset_postdata(); ?>
              <?php } ?>

              <?php if($sauna_object) { ?>
                  <?php $post = $sauna_object; ?>
                  <?php setup_postdata( $post );  ?>
                  <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></br>
                  <?php wp_reset_postdata(); ?>
              <?php } ?>
        </td>        

      </tr> 
      <?php endwhile; ?>

1 Answer 1

0

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>&nbsp</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;
6
  • Hey fischi..thanks for your quick reply man...i will try your solution tomorrow morning and get back to you...thanks a lot man..
    – Saif Islam
    Commented Feb 24, 2016 at 15:17
  • Hello fischi, I used your code exactly as it is...but now its printing out each wochentag name 4 times as its looping over the the time array..and it cannot extract post_title from the sauna and aufguss object...see the link please: imgur.com/lZOR8kS
    – Saif Islam
    Commented Feb 25, 2016 at 9:43
  • OK,I solved the wochentag problem...this line ($thead.= '<td>' . $wochentag . '</td>';) should be out of uhrzeit loop...but i still cannot extract the post_title from sauna and aufguss objects...please help me out
    – Saif Islam
    Commented Feb 25, 2016 at 9:59
  • hey fischi, i solved that too..it was my wochentag key which was making the problem...thanks for your wonderful and excellent solution...thanks a lot man...great work....cheers
    – Saif Islam
    Commented Feb 25, 2016 at 10:24
  • No problem, just wanted to update the answer. The problem is the wochentag was added to the $thead each time, when it should only be added once. Glad you figured it out :)
    – fischi
    Commented Feb 25, 2016 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.