简体   繁体   中英

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. 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

<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

enter image description here

please help me on this. i am new in 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. Bear in mind, this is only the algorithm. I will leave the actual HTML to you.

    $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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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