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 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/

Re: [Damon] Display list page grouping by date

By weblm - May 18, 2012

Damon thanks for the code.

It still doesn't solve the problem of wanting to have the items under the date be in an ordered list.

Basically I wanted to have the code be like this:

<h2>Month</h2>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>



This issue with the code is that you have to put everything in the foreach loop....so if the <ul>'s go in....they get repeated.

I ended up coming up with this code....it's a bit of a hack....but seems to work

<div class="newsletterlisting">
<?php $currentHeadlineDate = ''; ?>
<?php $counter = 0; ?>
<?php foreach ($tax_newsletterRecords as $record): ?>
<?php $thisRecordDate = date("F Y", strtotime($record['date'])); ?>
<?php if ($thisRecordDate != $currentHeadlineDate): ?>
<?php if ($counter > 0) { $counter = 0; echo "</ul>"; } ?>
<?php $currentHeadlineDate = $thisRecordDate; ?>
<a name="<?php echo $currentHeadlineDate ?>"></a>
<h2><?php echo $currentHeadlineDate ?></h2>
<ul>
<?php $counter = $counter + 1; ?>
<?php endif ?>
<?php foreach ($record['attachment'] as $index => $upload): ?><li><a href="<?php echo $upload['urlPath'] ?>" name="<?php echo $record['num'] ?>" target="_blank"><h1><?php echo htmlspecialchars($record['title']); ?></h1> <img src="/images/icon-pdf.png" width="13" height="15" border="0" alt="PDF" title="PDF" /></a><br /><span><?php echo htmlspecialchars($record['summary']); ?></span></li><?php endforeach ?>
<?php endforeach ?>
</ul>
</div>


Any suggestions for anyone on this would be great. It seems to be working.

-Kevin
LM