php计数并获取foreach循环中的最后一条记录

时间:2013-04-15 23:57:37

标签: php foreach count cakephp-1.3

我正在努力计算某一特定地点某一天(今天)的总事件数。 使用24小时的时间, 我试图在foreach循环中找到当天的最后一个事件,然后当当天最后一个事件的end_time比现在少时,显示文本,“所有预定的事件今天结束了。”

我的问题是xml数据包括所有房间中的所有事件14天而且我没有得到正确的计数。 我在foreach中有一些if语句可以减少它....

如果xml space id == room id,那么 如果xml事件start_date等于今天,那么 if($ i< $ numItems) 如果事件结束时间大于现在, 展示那些事件

当满足这些条件时,我需要记录的数量...当事件是今天的日期,并且在与通过url传递的roomID相同的房间中。 在foreach计算太多记录并将计数放在foreach中之前将$ numItems = $ count($ arr)放入,总是只返回1。

$arr = $this->data['events'];
$numItems = count($arr);
$i = 1;
foreach ($arr as $key => $reservation){
if ((int)$reservation->space_reservation->space_id == $roomID) 
{
//test today's date
        $resDT = date('m/d/y',strtotime($reservation->reservation_start_dt));
        if ($resDT == date('m/d/y'))
            {
            if ($i < $numItems)
            {
            $repst = substr($reservation->event_start_dt,11,5);
            $repet = substr($reservation->event_end_dt,11,5);

                if ( (date('H:i',strtotime($repet))) > (date('H:i')) )
                    {
                    echo '<h2>' .$reservation->event_name. '</h2> ';
                    echo '<span class="dates">' .date('m/d/y',strtotime($reservation->event_start_dt)). ' </span> ';
                    echo '<span class="times">' .date('g:i a',strtotime($repst)). ' - ';
                    echo date('g:i a',strtotime($repet)). '</span> ';
                    echo '<br />';
                    } // ends if event end date has passed

            //echo '<span style="color:#FFF200;">'.$i.'</span>';
            echo '<br />';
            }
//if $i equals last record
            elseif ($i === $numItems) {
                // test if last event ends show
                if ( (date('g:i a',strtotime($repet))) < (date('g:i: a')) ){
                echo '<span style="color:#FFF200;">All scheduled meetings/events have concluded for today. </span><br /><br />';
                }
            }
            $i++;
            } // ends if

    } // ends if
} // ends for each

2 个答案:

答案 0 :(得分:1)

以下代码将打印其中一个:

  1. 今天所有未完成的事件仅限于给定的房间;
  2. “所有预定活动已于今天结束。”如果没有剩下的话。
  3. 我添加了两个计数器,供您根据需要使用:

    • $ totalEventsToday - 计算在给定房间内今天安排的所有活动;
    • $ unfinishedEventsToday - 计算今天在结束日期/时间尚未过去的特定房间内安排的事件。

    所以这是代码:

    $today                 = date('m/d/y');
    $events                = $this->data['events'];
    $totalEventsToday      = 0;
    $unfinishedEventsToday = 0;
    
    // Check all reservations/events
    foreach ($events as $reservation) {
    
        // Process reservation/events for the given room only
        if ((int) $reservation->space_reservation->space_id == $roomID) 
        {
            // Process reservation/events for today only
            $resDT = date('m/d/y', strtotime($reservation->reservation_start_dt));
            if ($resDT == $today)
            {
                $totalEventsToday++;
    
                $repst = substr($reservation->event_start_dt,11,5);
                $repet = substr($reservation->event_end_dt,11,5);
    
                // Process unfinished event
                if ((date('H:i',strtotime($repet))) > (date('H:i')))
                {
                    $unfinishedEventsToday++;
    
                    echo '<h2>' .$reservation->event_name. '</h2> ';
                    echo '<span class="dates">' .date('m/d/y',strtotime($reservation->event_start_dt)). ' </span> ';
                    echo '<span class="times">' .date('g:i a',strtotime($repst)). ' - ';
                    echo date('g:i a',strtotime($repet)). '</span> ';
                    echo '<br />';
                } // ends if unfinished event
             } // ends if today
        } // ends if room
    } // ends for each
    
    // Test if there aren't any unfinished events left for today
    if (!$unfinishedEvents){
        echo '<span style="color:#FFF200;">All scheduled meetings/events have concluded for today. </span><br /><br />';
    }
    

答案 1 :(得分:0)

我想我不确定你的问题在哪里。你可以简单地添加一个计数器,就像我在下面$currentEvents所做的那样。

$arr = $this->data['events'];
$numItems = count($arr);
$currentEvents = 0;
$i = 1;
foreach ($arr as $key => $reservation){
if ((int)$reservation->space_reservation->space_id == $roomID) 
{
//test today's date
        $resDT = date('m/d/y',strtotime($reservation->reservation_start_dt));
        if ($resDT == date('m/d/y'))
            {
            if ($i < $numItems)
            {
            $repst = substr($reservation->event_start_dt,11,5);
            $repet = substr($reservation->event_end_dt,11,5);

                if ( (date('H:i',strtotime($repet))) > (date('H:i')) )
                    {
                    $currentEvents++;
                    echo '<h2>' .$reservation->event_name. '</h2> ';
                    echo '<span class="dates">' .date('m/d/y',strtotime($reservation->event_start_dt)). ' </span> ';
                    echo '<span class="times">' .date('g:i a',strtotime($repst)). ' - ';
                    echo date('g:i a',strtotime($repet)). '</span> ';
                    echo '<br />';
                    } // ends if event end date has passed

            //echo '<span style="color:#FFF200;">'.$i.'</span>';
            echo '<br />';
            }
//if $i equals last record
            elseif ($i === $numItems) {
                // test if last event ends show
                if ( (date('g:i a',strtotime($repet))) < (date('g:i: a')) ){
                echo '<span style="color:#FFF200;">All scheduled meetings/events have concluded for today. </span><br /><br />';
                }
            }
            $i++;
            } // ends if

    } // ends if
} // ends for each