繁体   English   中英

如何在 PHP 中使用 foreach 循环仅回显特定记录一次?

[英]How to echo specific records only once using foreach loops in PHP?

我在 PHP 中使用 foreach 循环获得这些输出。现在 foreach 中的 output 如下所示。 如果下面的空档日期匹配,那么我们必须在单独的表格中显示基于相同日期的空档时间。

$eventSlotInfo = $block->getEventSlotDetails();
foreach ($eventSlotInfo as $slot) {
     echo "<PRE>";
     print_r($slot->getData());
}

我的数组

Array
(
    [entity_id] => 3
    [event_id] => 16
    [slot_date] => 2022-04-17 05:00:00
    [slot_start_time] => 05:00:00
    [slot_end_time] => 06:00:00
    [slot_limit] => 33
    [created_at] => 2022-04-18 07:47:00
    [updated_at] => 2022-04-18 07:47:00
)
Array
(
    [entity_id] => 4
    [event_id] => 16
    [slot_date] => 2022-04-17 05:00:00
    [slot_start_time] => 06:00:00
    [slot_end_time] => 06:00:00
    [slot_limit] => 33
    [created_at] => 2022-04-18 07:47:00
    [updated_at] => 2022-04-18 07:47:00
)

Array
(
    [entity_id] => 6
    [event_id] => 16
    [slot_date] => 2022-04-18 05:00:00
    [slot_start_time] => 07:00:00
    [slot_end_time] => 06:00:00
    [slot_limit] => 33
    [created_at] => 2022-04-18 07:47:00
    [updated_at] => 2022-04-18 07:47:00
)
Array
(
    [entity_id] => 7
    [event_id] => 16
    [slot_date] => 2022-04-18 05:00:00
    [slot_start_time] => 08:00:00
    [slot_end_time] => 09:00:00
    [slot_limit] => 33
    [created_at] => 2022-04-18 07:47:00
    [updated_at] => 2022-04-18 07:47:00
)

我的 HTML

<section class='slot_blk'>
        <table class='cst_table header_table'>
            <thead>
                <th>Date</th>
                <th>total registataions</th>
                <th>total attanded</th>
                <th>total Not attanded</th>
                <th>Status</th>
            </thead>
            <tbody>
                <tr>
                    <td>17-05-06 04:55:5</td>
                    <td>170</td>
                    <td>89</td>
                    <td>80</td>
                    <td class='live'>Live</td>
                </tr>
            </tbody>
        </table>
    <?php //} ?>
        <table class='cst_table'>
            <thead>
                <th>Slot time</th>
                <th>No of registataions</th>
                <th>Attanded</th>
                <th>Not Attanded</th>
                <th>Action</th>
            </thead>
            <tbody>
                <tr>
                    <td>
                        "05:00:00"
                    </td>
                    <td>170</td>
                    <td>89</td>
                    <td>80</td>
                    <td class='action'>View</td>
                </tr>
            </tbody>
        </table>
    </section>

Output 像这样

在此处输入图像描述

请帮我解决这个问题。 我是 php 的新人。

假设数据按日期排序,您可以检查日期何时更改,并使用它来对一天的所有事件进行分组,并且只有 output 次。 请记住,这只是算法。 我会把实际的 HTML 留给你。

    $currDate = null;
    foreach ( $eventSlotInfo as $slot ) {
        list($date, $time) = explode(' ', $slot['slot_date']);
        // this only gets output when the date changes.
        // Because $currDate starts off as null, this will output
        // the first time through the loop.
        // This only works if the data is sorted by date
        if ( $date != $currDate ) {
            // output date
        }
        // output time
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM