Support

Account

Home Forums Add-ons Repeater Field Display Range of Dates with First and Last Row in Repeater

Solved

Display Range of Dates with First and Last Row in Repeater

  • Using the Date Picker field in a Repeater field, I’m trying to display a range of dates. The last row has the oldest date, and the first row has the latest date. You can see it partially working on my archive page – http://dev.foursquarechurch.info/sermons/. Here’s my code so far:

    <?php
    
    $rows = get_field( 'sermon_series_media' ); // Get all the rows
    $first_row_date = $rows[0]['sermon_date']; // Get the first row
    $last_row = end( $rows );
    $last_row_date = $last_row['sermon_date']; // Get the sub field value
    
    if( $last_row_date ) : ?>
    
        <div class="archive-sermon-date-range">
    
        <?php echo last_row_date; ?>
    
            <?php if( $last_row_date != $first_row_date ) : ?> - <?php echo $first_row_date; ?>
    
            <?php endif; ?>
    
        </div>
    
    <?php endif; ?>

    As you can see on the Peace Makers series, I get a warning. The Peace Makers series doesn’t have any data in the Repeater field – There are no rows.

    Warning: end() expects parameter 1 to be array, boolean given in…

    I’m very new to PHP. Any help would be appreciated.

  • Only output content if there are rows

    
    <?php 
      $rows = get_field( 'sermon_series_media' ); // Get all the rows
      if ($rows) {
        $first_row_date = $rows[0]['sermon_date']; // Get the first row
        $last_row = end( $rows );
        $last_row_date = $last_row['sermon_date']; // Get the sub field value
    
        if( $last_row_date ) : 
          ?>
            <div class="archive-sermon-date-range">
              <?php 
                echo last_row_date;
                if( $last_row_date != $first_row_date ) : 
                  ?> - <?php echo $first_row_date;
                php endif; 
              ?>
            </div>
          <?php
        endif;
      } // end if $rows
    
  • That works. Thanks, John!

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

The topic ‘Display Range of Dates with First and Last Row in Repeater’ is closed to new replies.