简体   繁体   English

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

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

I have these outputs using foreach loop in PHP. Right now the output inside foreach is like below.我在 PHP 中使用 foreach 循环获得这些输出。现在 foreach 中的 output 如下所示。 if the below slot date matches then we have to show the slot time based on same date in a separate table.如果下面的空档日期匹配,那么我们必须在单独的表格中显示基于相同日期的空档时间。

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

My array我的数组

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
)

MY HTML我的 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 Like this Output 像这样

enter image description here在此处输入图像描述

please help me on this.请帮我解决这个问题。 i am new in php.我是 php 的新人。

Assuming the data is ordered by date, you could check for when the date changes and use that to group all the events for a day and only output the times.假设数据按日期排序,您可以检查日期何时更改,并使用它来对一天的所有事件进行分组,并且只有 output 次。 Bear in mind, this is only the algorithm.请记住,这只是算法。 I will leave the actual HTML to you.我会把实际的 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