@php
$today = date('Y-m-d');
$events = DB::select("SELECT * FROM event WHERE end_date <= '$today' ORDER BY start_date ASC");
@endphp
@foreach($events as $event)
@php
$start = new DateTime($event->start_date);
$month = $start->format('M d');
$day = $start->format('D');
$time = $start->format('g:ia');
@endphp
<div class="d-flex flex-row mb-3 align-items-top">
<div class="col-2 px-0">
<div class="div sticky-top">
<h6 class="mb-0">{{ $month }}</h6><small>{{ $day }}</small>
</div>
</div>
<div class="col-10">
<div class="col-12 mb-4">
<span style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{ $time }} • {{ $event->location }}</span>
<h6 style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis"><a href="edit_event?event_id={{ $event->event_id }}" class="text-dark">{{ $event->name }}</a></h6>
</div>
</div>
</div>
@endforeach
嘿,
每当一个新事件出现时,我都希望它以某种方式进行排序:如果一个事件在同一日期发生,那么该日期本身就不会重复,就像您在图像中看到的那样。
我已经尝试解决了很长时间,我知道它是可能的,因为它类似于Facebook的活动页面。
答案 0 :(得分:1)
您可以按日期对事件进行分组:
$arr = [];
foreach($events as $event)
{
$key = $event->start_date;
if (array_key_exists($key; $arr)
{
array_push($arr[$key]; $event);
} else {
$arr[$event->start_date] = $event;
}
}
比起您想去的@foreach($arr as $date)
,$date
将是一个数组,其中包含与特定日期相关的事件。
最后,
@foreach($arr as $date)
@foreach($date as $event)
@php
$start = new DateTime($event->start_date);
$month = $start->format('M d');
$day = $start->format('D');
$time = $start->format('g:ia');
@endphp
<div class="d-flex flex-row mb-3 align-items-top">
<div class="col-2 px-0">
<div class="div sticky-top">
<h6 class="mb-0">{{ $month }}</h6><small>{{ $day }}</small>
</div>
</div>
<div class="col-10">
<div class="col-12 mb-4">
<span style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{ $time }} • {{ $event->location }}</span>
<h6 style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis"><a href="edit_event?event_id={{ $event->event_id }}" class="text-dark">{{ $event->name }}</a></h6>
</div>
</div>
</div>
@endforeach
@endforeach
答案 1 :(得分:0)
您应该有一个全局变量,例如$lastdate
。然后,当您创建一个新日期时,请检查它是否等于$ lastdate。如果是,则不记录日期;如果不是,则记录日期,然后(!)将$ lastdate更新为新日期。
答案 2 :(得分:0)
您还可以使用Laravel集合groupBy()
并根据事件start_date
对事件进行分组。例如:
@php
$today = date('Y-m-d');
$events = DB::table('event')
->where('end_date', '<=', $today)
->orderBy('start_date', 'asc')
->get()
->groupBy('start_date');
@endphp
@foreach($events as $date => $event)
@php
$start = new DateTime($date);
$month = $start->format('M d');
$day = $start->format('D');
$time = $start->format('g:ia');
@endphp
<div class="d-flex flex-row mb-3 align-items-top">
<div class="col-2 px-0">
<div class="div sticky-top">
<h6 class="mb-0">{{ $month }}</h6><small>{{ $day }}</small>
</div>
</div>
<div class="col-10">
<div class="col-12 mb-4">
<span style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis">{{ $time }} • {{ $event->location }}</span>
<h6 style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis"><a href="edit_event?event_id={{ $event->event_id }}" class="text-dark">{{ $event->name }}</a></h6>
</div>
</div>
</div>
@endforeach
此外,您不应该在视图中编写此逻辑。它应该是您的控制器的一部分。