[英]How to echo specific multiple same records only once using foreach loops in PHP?
[英]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.