Support

Account

Home Forums Add-ons Repeater Field future dates table in single.php

Solved

future dates table in single.php

  • Hello,

    I created an eventtable for the single.php which works fine so far. But I would like to show only future events ($date_beginn)

    Could somebody help me please ?
    Thaaankssss!!!

             <?php
    
            // check if the repeater field has rows of data
            if( have_rows('t_block') ):
    
            echo "<table>";
    
                 // loop through the rows of data
                while ( have_rows('t_block') ) : the_row();              
                    
    
            $date_beginn = DateTime::createFromFormat('Ymd', get_sub_field('beginn'));
            $date_ende= DateTime::createFromFormat('Ymd', get_sub_field('ende'));
            $preis= get_sub_field('preis');  
            $preis_ez= get_sub_field('ez_preis');
            $buchbar= get_sub_field('buchbar'); 
            $sprache= get_sub_field('sprache');   
            $anfrage= get_sub_field('anfrage');   
            $bemerkung= get_sub_field('bemerkung');   
            $garantiert= get_sub_field('garantiert');  
    
                    // display a sub field value
    
                  echo '<tr> <td>' . $buchbar . '</td>';
                      echo '<td>' . $date_beginn->format('d.m.Y') . '</td>';         
                       echo '<td>' . $date_ende->format('d.m.Y') . '</td>';       
                       echo '<td><strong>' . $preis . '  EUR </strong> </td>';
                            echo '<td>' . $preis_ez . '</td>';
                        echo '<td>' . $sprache . '</td>';
                      echo '<td>' . $garantiert . '</td>';
                       echo '<td>' . $anfrage . '</td>';
                   echo '<td>' . $bemerkung . '</td> </tr>';
                endwhile;
    
            echo "</table>";
    
            else :
    
            endif;
    
        ZitierenZitieren
    
  • Basically you need to compare the date it is now with the end date. This is not exact but how I might do it. Hopefully it point you in the right direction.

    
    while ( have_rows('t_block') ) : the_row(); 
        $now = strtotime(date('Y-m-d', time()));
        // I am assuming that your date is stored as 'Y-m-d'
        // see strtotime php doc form more information
        $end_time - strtotime(get_sub_field('ende'));
        if ($now > $end_time) {
        // not in the future, go to the next row
            continue;
        }
        // the rest of your code here ...
    
    
  • Dear John,

    thank you for your reply.
    Do you mean it like this? There must be a mistake somewhere…
    maybe with the { ?
    Sorry I´m an absolute newbie in php…

    <?php
    
    while ( have_rows('t_block') ) : the_row(); 
        $now = strtotime(date('Y-m-d', time()));
        // I am assuming that your date is stored as 'Y-m-d'
        // see strtotime php doc form more information
        $end_time - strtotime(get_sub_field('ende'));
        if ($now > $end_time) {
        // not in the future, go to the next row
            continue;
        }
    
    // check if the repeater field has rows of data
    if( have_rows('t_block') ):
    
    echo '<h2>' . 'Reisetermine' . '</h2>';
    echo "<table>";
    echo '<tr> <th>'.''.'</th>';
    echo '<th>' . '' . '</th>';
    echo '<th>' . Anreise . '</th>';
    echo '<th>'.''.'</th>';
    echo '<th>' . Abreise . '</th>';
    echo '<th>' . 'Preis p.P. <br> im DZ' . '</th>';
    echo '<th>' . '' . '</th>';
    echo '<th>' . '' . '</th>';
    echo '<th>' . '' . '</th>';
    echo '<th>' . '' . '</th>';
    echo '<th>' . '' . '</th></tr>';
    
     	// loop through the rows of data
        while ( have_rows('t_block') ) : the_row();              
    		
    
    $date_beginn = DateTime::createFromFormat('Ymd', get_sub_field('beginn'));
    $date_ende= DateTime::createFromFormat('Ymd', get_sub_field('ende'));
    $preis= get_sub_field('preis');  
    $preis_ez= get_sub_field('ez_preis');
    $buchbar= get_sub_field('buchbar'); 
    $sprache= get_sub_field('sprache');   
    $anfrage= get_sub_field('anfrage');   
    $bemerkung= get_sub_field('bemerkung');   
    $garantiert= get_sub_field('gesichert');  
    
            // display a sub field value
    
          echo '<tr><td>' . $garantiert . '</td>';
    	  echo '<td>' . $buchbar . '</td>';
          echo '<td>' . $date_beginn->format('d.m.Y') . '</td>'; 		
              echo '<td>' . '-' . '</td>';
        echo '<td>' . $date_ende->format('d.m.Y') . '</td>';       
        echo '<td><strong>' . $preis . '  € </strong> </td>';
        echo '<td>' . $preis_ez . ' </td>';
        echo '<td>' . $sprache . '</td>';
        echo '<td><strong>' . $anfrage . '</strong></td>';
       	echo '<td>' . $bemerkung . '</td> </tr>';
        endwhile;
    
    echo "</table>";
    
    else :
    
    echo get_post_meta($post->ID, 'traveldates' ,true); 
    
    endif;
    
    ?>  
                 
  • The code I posted should have been put after the while statement you already had, something like this.

    
    
    <?php
    
            // check if the repeater field has rows of data
            if( have_rows('t_block') ):
    
            echo "<table>";
    
                 // loop through the rows of data
                while ( have_rows('t_block') ) : the_row();   
    
    while ( have_rows('t_block') ) : the_row(); 
        $now = strtotime(date('Y-m-d', time()));
        // I am assuming that your date is stored as 'Y-m-d'
        // see strtotime php doc form more information
        $end_time - strtotime(get_sub_field('ende'));
        if ($now > $end_time) {
        // not in the future, go to the next row
            continue;
        }
    

    I was showing you where to put it by including the while in my code example rather than copying and pasting all of your code.

  • Dear John,

    thank you for your reply.
    I´m sorry, but when I put in your code, the table won’t show any dates. All empty. 🙁

  • You’re probably going to need to debug the lines where I calulate the current time and the end time. I’m not sure how you are storing dates and I am unfamiliar with how you are displaying dates.

    I did just notice this though

    this line

    $end_time - strtotime(get_sub_field('ende'));

    should be

    $end_time = strtotime(get_sub_field('ende'));

    There should be an =‘ not a ‘ – ‘

  • YES IT WORKS !!! THANK YOU VERYYY MUCH !!!!!! 🙂

  • Dear John,

    one more question please.

    What do I have to do, when I would like to publish only the next 5 events and not all ?

  • Before your loop create a counter, increment it and then break when it gets to 6

    
    $count = 0;
    while ( have_rows('t_block') ) : the_row(); 
        $now = strtotime(date('Y-m-d', time()));
        // I am assuming that your date is stored as 'Y-m-d'
        // see strtotime php doc form more information
        $end_time - strtotime(get_sub_field('ende'));
        if ($now > $end_time) {
        // not in the future, go to the next row
            continue;
        }
        $count++;
        if ($count > 5) {
          // stop showing events
          break;
        }
    
  • Thank you !!! It works 🙂

  • Hello John, or somebody else,

    could you please help me, how to add a custom filter in the frontpage to select the category ?

    Thanks a lot
    Taro

Viewing 11 posts - 1 through 11 (of 11 total)

The topic ‘future dates table in single.php’ is closed to new replies.