vendredi 27 avril 2018

How to display a block of code at the end of a foreach loop only once

Below, I have the following foreach statement with an if statement nested inside of it. I am running into an issue that I cannot figure out.

I am wanting the following code to be included in the else-statement, but not be in the foreach loop. I have tried the break function, but it breaks the loop, which I do not want. I also tried changing the structure of the loop to include a endforeach before the end bracket for the else-statement, but this just broke the code.

Essentially, I am just wanting my foreach loop to run normal, but then for the else-statement to populate the moreEventsContainer. I can't think of a way to do it.

Does anyone have any ideas?

  echo '<div class="moreEventsContainer">
          <div id="moreEventsWrap" class="total-center">
           <span class="moreEventsLink">SEE ALL EVENTS</span>
           <div class="rightArrow"></div>
         </div>
       </div>'

;

This is what I am wanting to output:

enter image description here

Without the break, it does this:

enter image description here

With the break, I only get this:

enter image description here

Full Code:

foreach ($event_rows as $event_row) {
    $event_name = $event_row['event_name'];
    $display_date = $event_row['display_date'];
    $event_description = $event_row['small_desc'];
    $end_date = new DateTime($event_row['end_date']);
    $date = new DateTime('now');
    if ($date >= $end_date) {
        //$noEvents = 'No events are scheduled yet.';
        $noEvents = '
        <div id="noEvents">
        </div>
        ';
    } else {
        echo '<div class="eventBlock">';
        echo '<div class="total-center eventBlockWrap">';
        echo '<span class="displayDate">'. $display_date .'</span>';
        echo '<span class="eventName">'. $event_name .'</span>';
        echo '<p class="dGsmall margNone">'. $event_description .'</p>';
        echo '</div>';
        echo '</div>';
        break;
        echo '<div class="moreEventsContainer">
                <div id="moreEventsWrap" class="total-center">
                    <span class="moreEventsLink">SEE ALL EVENTS</span>
                    <div class="rightArrow"></div>
                </div>
            </div>'
        ;

} }

Aucun commentaire:

Enregistrer un commentaire