Upcoming events, with all of each day's events under one column of li

11 posts by 2 authors in: Forums > CMS Builder
Last Post: December 17, 2014   (RSS)

By claire - December 16, 2014

Ah, foreach loops. I love them really.

Change your loop like this:

<?php foreach($eventsRecords as $idx => $record): 
$showDate = false; 
//$formatted_date = date("l, M jS Y", strtotime($record['date']));  // Add a ,Y to display the year as well. Change M to F for full month's name
$formatted_date = date("M d", strtotime($record['date']));  // Add a ,Y to display the year as well. Change M to F for full month's name
if ($formatted_date != $lastDate): 
$lastDate = $formatted_date; 
$showDate = true; 
endif; ?>

<li class="upcoming-event-activity" style="margin-left:0px; padding-left:0px;">
<h4><?php echo $formatted_date; ?></h4>
<?php /*?><?php echo date("D, M jS, Y g:i:s a", strtotime($record['date'])) ?><?php */?> 
<h5 style="font-weight:400; padding-top:16px;"><a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a></h5>
<!-- /upcoming-event-activity -->
 
if(isset($eventsRecords[$idx+1]) : ?>
<?php $nextDate = date('Y-m-d', strtotime($eventsRecords[$idx+1]['date']));
$currDate = date('Y-m-d', strtotime($record['date']));
if($nextDate != $currDate) : ?>
</li>
<?php endif; ?>
<?php else : ?>
</li>
<?php endif; ?>
       
<?php endforeach ?>

This should close the <li> tag if the next event is not on the same day, and leave it open otherwise. Hopefully I've got the logic right - if not, let me know and I'll take a look at it again.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Mikey - December 16, 2014 - edited: December 16, 2014

Hey Claire,

I tried your solution, but it's throwing the following error message:

Parse error: syntax error, unexpected T_ELSE in /home/content/37/9336237/html/bigoakcreativeus/tomh/indexClaire.php on line 1402

any thought on how to fix this?

By claire - December 16, 2014

Yep, I got the logic wrong. Try this loop instead, it should have no more syntax errors:

<?php foreach($eventsRecords as $idx => $record):
$showDate = false;
//$formatted_date = date("l, M jS Y", strtotime($record['date'])); // Add a ,Y to display the year as well. Change M to F for full month's name
$formatted_date = date("M d", strtotime($record['date'])); // Add a ,Y to display the year as well. Change M to F for full month's name
if ($formatted_date != $lastDate):
$lastDate = $formatted_date;
$showDate = true;
endif; ?>

<li class="upcoming-event-activity" style="margin-left:0px; padding-left:0px;">
<h4><?php echo $formatted_date; ?></h4>
<?php /*?><?php echo date("D, M jS, Y g:i:s a", strtotime($record['date'])) ?><?php */?>
<h5 style="font-weight:400; padding-top:16px;"><a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a></h5>
<!-- /upcoming-event-activity -->

<?php if(isset($eventsRecords[$idx+1])) : ?>
<?php $nextDate = date('Y-m-d', strtotime($eventsRecords[$idx+1]['date']));
$currDate = date('Y-m-d', strtotime($record['date']));
if($nextDate != $currDate) : ?>
</li>
<?php endif; ?>
<?php else : ?>
</li>
<?php endif; ?>

<?php endforeach ?>

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Mikey - December 16, 2014

Hey Claire,

That fixed the error, but the events are still listed as individual list items... so for example the two events for Dec 30 are two separate list items -VS- being grouped together. I've attached a snap shot to show how it appears now. Any other suggestions on how to get this working?

Thanks for the help!

Zick

By Mikey - December 16, 2014

Hey Claire,

So I was looking at the back-end source code and here's what I think is going on. It's retaining the leading code for same Month/Day events listed below - as seen in bold.

<li class="upcoming-event-activity" style="margin-left:0px; padding-left:0px;">
<h4>Dec 30</h4>
 
<h5 style="font-weight:400; padding-top:16px;"><a href="/event_details.php?Family-Festival-in-the-park-56">Family Festival in the park</a></h5>
<!-- /upcoming-event-activity -->



<li class="upcoming-event-activity" style="margin-left:0px; padding-left:0px;">
<h4>Dec 30</h4>
 
<h5 style="font-weight:400; padding-top:16px;"><a href="/event_details.php?City-Council-Meeting-13">City Council Meeting</a></h5>
<!-- /upcoming-event-activity -->

</li>


<li class="upcoming-event-activity" style="margin-left:0px; padding-left:0px;">
<h4>Aug 20</h4>
 
<h5 style="font-weight:400; padding-top:16px;"><a href="/event_details.php?Spring-Fesitval-38">Spring Fesitval</a></h5>
<!-- /upcoming-event-activity -->

</li>


<li class="upcoming-event-activity" style="margin-left:0px; padding-left:0px;">
<h4>Sep 23</h4>
 
<h5 style="font-weight:400; padding-top:16px;"><a href="/event_details.php?Family-Festival-in-the-park-55">Family Festival in the park</a></h5>
<!-- /upcoming-event-activity -->

</li>

Yet implementing your solution on the ending <li> just fine as seen in the bolded code below:


<li class="upcoming-event-activity" style="margin-left:0px; padding-left:0px;">
<h4>Dec 30</h4>
 
<h5 style="font-weight:400; padding-top:16px;"><a href="/event_details.php?Family-Festival-in-the-park-56">Family Festival in the park</a></h5>
<!-- /upcoming-event-activity -->



<li class="upcoming-event-activity" style="margin-left:0px; padding-left:0px;">
<h4>Dec 30</h4>
 
<h5 style="font-weight:400; padding-top:16px;"><a href="/event_details.php?City-Council-Meeting-13">City Council Meeting</a></h5>
<!-- /upcoming-event-activity -->

</li>


<li class="upcoming-event-activity" style="margin-left:0px; padding-left:0px;">
<h4>Aug 20</h4>
 
<h5 style="font-weight:400; padding-top:16px;"><a href="/event_details.php?Spring-Fesitval-38">Spring Fesitval</a></h5>
<!-- /upcoming-event-activity -->

</li>


<li class="upcoming-event-activity" style="margin-left:0px; padding-left:0px;">
<h4>Sep 23</h4>
 
<h5 style="font-weight:400; padding-top:16px;"><a href="/event_details.php?Family-Festival-in-the-park-55">Family Festival in the park</a></h5>
<!-- /upcoming-event-activity -->

</li>

So the trick from here is to remove the second instance for a same Month/Day event's leading <li> and <h4> - for a single instance of these for events falling on the same Month/Day. So the code would ultimately look like this:


<li class="upcoming-event-activity" style="margin-left:0px; padding-left:0px;">
<h4>Dec 30</h4>
 
<h5 style="font-weight:400; padding-top:16px;"><a href="/event_details.php?Family-Festival-in-the-park-56">Family Festival in the park</a></h5>
<!-- /upcoming-event-activity -->
 
<h5 style="font-weight:400; padding-top:16px;"><a href="/event_details.php?City-Council-Meeting-13">City Council Meeting</a></h5>
<!-- /upcoming-event-activity -->

</li>


<li class="upcoming-event-activity" style="margin-left:0px; padding-left:0px;">
<h4>Aug 20</h4>
 
<h5 style="font-weight:400; padding-top:16px;"><a href="/event_details.php?Spring-Fesitval-38">Spring Fesitval</a></h5>
<!-- /upcoming-event-activity -->

</li>


<li class="upcoming-event-activity" style="margin-left:0px; padding-left:0px;">
<h4>Sep 23</h4>
 
<h5 style="font-weight:400; padding-top:16px;"><a href="/event_details.php?Family-Festival-in-the-park-55">Family Festival in the park</a></h5>
<!-- /upcoming-event-activity -->

</li>

By claire - December 16, 2014

Can you send me a link to the page that this is on, and attach a copy here? I'll take another look.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Mikey - December 16, 2014

Hey Claire, I sent a link to you via email.

By claire - December 17, 2014

Thanks for that. I've moved some of the logic around, and now that page works as it should. Take a look and let me know what you think.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Mikey - December 17, 2014

Thanks Claire - your the best!

I really appreciate the help with this. So what do I owe Interactive Tools for fixing this for me? Send me a bill for your time.

Cheers,
Zick