So I had a summer festival website where the client requested that I output a title section for each new day of events. Basically, we were outputting little event snippets and the page was very long and we felt like the user was going to get lost in all the upcoming dates.
Because we were outputting the events in a loop, I had to figure out a solution that could accomplish this while running through each event in the loop.
So I whipped up this little snippet to store any values that had already been echoed in the loop.
Then I checked to see if they had already been output.
If not, I output the date and then stored it in the “already echoed” array and moved on.
This is basically a simple way to organize events when there are multiple events in a single day.
<?php | |
// Example using a while loop (inside WordPress in this case) | |
$already_echoed = array() | |
while ( have_posts() ) : the_post(); | |
if (!in_array($date, $already_echoed)) { //check if current date is in the already_echoed array | |
echo $date; | |
} | |
$already_echoed[] = $date; //store all dates in an array to check against. | |
// Example using a foreach loop | |
$already_echoed = array(); | |
foreach ($events as $event) { | |
if (!in_array($event->date, $already_echoed)) { | |
echo $event->date . "<br />"; | |
} | |
$already_echoed[] = $event->date; | |
} |
Leave a Reply