Group By Date

2 posts by 2 authors in: Forums > CMS Builder
Last Post: May 13, 2010   (RSS)

Re: [concrete] Group By Date

By Chris - May 13, 2010

Hi concrete,

To accomplish this, you need to have your records sorted by date, then use a variable to keep track of the last date "headline" displayed; while looping through your records, if the current record's date does not belong to the current headline, display a new headline. Here's an example:

<?
// load records
list($articleRecords, ) = getRecords(array(
'tableName' => 'article',
'orderBy' => 'createdDate',
));
?>

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


If you need help adapting the code to your section, please post a list viewer for your section (so I can see what your fields are, etc.) and I'll show you how to get this working.

I hope this helps! Please let me know if you have any questions.
All the best,
Chris