jeudi 26 avril 2018

Breaking out of a foreach within a nested if statement

I am having a difficult time figuring out how to break a section out of a an else-statement that is nested within a foreach loop.

The section I want to break out of the foreach loop is :

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

The thing is that it is part of the else statement, but leaving it in the loop just makes it duplicate itself over and over when I just want it to appear once.

Does anyone see how I can do this?

I tried doing the alternative endforeach nested before the end of the else statement, but it just broke the code.

Any ideas?

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>';
            echo '<div class="moreEventsContainer">
                    <div id="moreEventsWrap" class="total-center">
                        <span class="moreEventsLink">SEE ALL EVENTS</span>
                        <div class="rightArrow"></div>
                    </div>
                </div>'
            ;
        }
    }
    if ($noEvents != NULL) {
        echo $noEvents;
    } else {

    }

Aucun commentaire:

Enregistrer un commentaire