Display list page grouping by date

4 posts by 2 authors in: Forums > CMS Builder
Last Post: May 18, 2012   (RSS)

By weblm - May 16, 2012

Can't figure this one out. I have a multi article section that simply lists newsletters. Each record has a "date" field.

What I want to display on the page is all the articles.....grouped by date like this:

MAY 2012
- article
- article

APRIL 2010
- article
- article


etc.....

What I want is the MONTH YEAR written out....and then the articles placed in a unordered list.

I have this code that works.....the only problem is....since the MONTH YEAR names are done within the foreach.....I have no place to put the <ul></ul>

<?php $currentHeadlineDate = ''; ?>
<?php foreach ($tax_newsletterRecords as $record): ?>
<?php $thisRecordDate = date("F Y", strtotime($record['date'])); ?>
<?php if ($thisRecordDate != $currentHeadlineDate): ?>
<?php $currentHeadlineDate = $thisRecordDate; ?>
<h2><?php echo $currentHeadlineDate ?></h2>
<?php endif ?>
<?php echo htmlspecialchars($record['title']) ?><br/>
<?php endforeach ?>



Any ideas on how this can be changed to allow for the headings (MONTH YEAR) to be outside a foreach loop so I can put each article in a list?

Thanks.

-Kevin
LM

Re: [kblm] Display list page grouping by date

By weblm - May 16, 2012

Also......I need to have the code be able to only show for example....the last 4 months worth of groups.

I'm completely stumped.

-Kevin
LM

Re: [kblm] Display list page grouping by date

By Damon - May 17, 2012

Hi Kevin,

Here is some code that will display the month year followed by records from that date like this:

May 2012
Article Name
Article Name
Article Name

April 2012
Article Name
Article Name
Article Name

etc.

<?php foreach ($newsRecords as $record): ?>

<?php //** DEFINE $DATE AS NEW VARIABLE FOR DATE IN RECORD: ?>
<?php $date = date("F Y", strtotime($record['date'])); ?>
<?php //** DISPLAY DATE IF DIFFERENT FROM LAST DATE ?>
<?php if (@$lastDate != $date): ?>
<br />
<strong><?php echo $date ?></strong>
<br />
<?php endif ?>

<?php //** SHOW WHAT EVER OTHER CONTENT I WANT FROM RECORDS ?>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a><br />

<?php //** SET $LASTDATE VARIABLE ?>
<?php $lastDate = $date; ?>

<?php endforeach; ?>


You can remove the COMMENT LINES.

The last step is to make sure that you when you are loading the records at the top of page code you have the 'orderBy' set to sort by date like this:

'orderBy' => 'date DESC',

Try this out and let me know if this is what you are after.

Thanks!
Cheers,
Damon Edis - interactivetools.com

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