如何在foreach循环结束时只显示一次代码块

时间:2018-04-27 14:30:05

标签: php if-statement pdo foreach

下面,我有以下foreach语句,其中嵌套了if语句。我遇到了一个我无法弄清楚的问题。

我希望以下代码包含在else-statement中,但不在foreach循环中。我尝试过break函数,但它打破了循环,我不想要。我还尝试更改循环的结构,以便在endforeach的结束括号之前包含else-statement,但这只是破坏了代码。

基本上,我只是想让我的foreach循环正常运行,然后让else-statement填充moreEventsContainer。我无法想办法。

有没有人有任何想法?

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

这就是我想输出的内容:

enter image description here

没有休息,它会这样做:

enter image description here

休息时,我只能得到这个:

enter image description here

完整代码:

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>'
        ;
    }
}

1 个答案:

答案 0 :(得分:2)

尝试这样的事情,只需跟踪是否有任何事件,然后回显循环外的最后一个事件。

$AtLeastOneEvent = false;
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 {
        $AtLeastOneEvent = true;
        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>';           
     }
}

if ($AtLeastOneEvent == true) 
{
 echo '<div class="moreEventsContainer">
                <div id="moreEventsWrap" class="total-center">
                    <span class="moreEventsLink">SEE ALL EVENTS</span>
                    <div class="rightArrow"></div>
                </div>
            </div>';
}