Reputation: 505
@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
Hey all,
Whenever a new event comes into view, I want it to be sorted in a way that if an event falls on the same date then the date itself will not be repeated, just as you can see in the image.
I've tried figuring it out for so long, I know it's possible as it's similar to Facebook's event page.
Upvotes: 2
Views: 59
Reputation: 8750
You could also use Laravel Collections groupBy()
and group the events according to their start_date
. For example:
@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
Also, you shouldn't be writing this logic in your view. It should be part of your Controller.
Upvotes: 0
Reputation: 2398
You can group events by date:
$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;
}
}
Than you want to go @foreach($arr as $date)
and $date
would be an array with events connected with certain date.
Finally,
@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
Upvotes: 1
Reputation: 259
You should have a global variable, something like $lastdate
. Then, when you create a new date, check if it's equal to $lastdate. If it is, don't log the date, if it isn't, log the date and (!) update $lastdate to the new date.
Upvotes: 0