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: [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