List Filtering Question

4 posts by 3 authors in: Forums > CMS Builder
Last Post: June 19, 2009   (RSS)

By greatjakes - June 18, 2009

hi, I have news list page that is sorted by date. Here is link to the mockup page: http://gjdemo.com/irving/news.htm
I want to display the years from the date field as links and filter the page by year when the link is clicked.(Ex: View All, 2009, 2008, 2007 and so on).
I was wondering if there is a way to achieve this without having to create different viewpages for each year? Thanks!

This is what I have for the list page -

<?php foreach ($newsRecords as $record): ?>
php echo date("m/d/y", strtotime($record['date'])) ?>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a>
<?php endforeach; ?>

Re: [greatjakes] List Filtering Question

By ross - June 18, 2009

Hi there.

Thanks for posting!

What stage are you at here? It's going to be best to break this down into smaller pieces.

I would start with getting a list of the years to appear down the left side. Once you get that going, we can figure out what the link should be.

Do you already have the years appearing? Let me know :).
-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

Re: [greatjakes] List Filtering Question

By Damon - June 19, 2009

Hi,

You could just hard code the year links for the left nav but to have the year links automatically add new year links every year, here is some code to try:

<?php foreach ($newsRecords as $record): ?>
<?php $years[] = date("Y", strtotime($record['date'])) ?>
<?php endforeach; ?>
<?php $years = array_unique($years); ?>
<?php foreach ($years as $year): ?>
<a href="/news.php?date_year=<?php echo $year; ?>"><?php echo $year; ?></a> <br />
<?php endforeach; ?>


Make any changes to variables and array names as needed to match your setup.

The first foreach loop grabs just the article years and puts them into an array.

The next line uses the array_unique to put only years into the new array that are unique (so there will be no duplicates).

Then then final foreach loop outputs all the unique years including a search query link.

Let me know how that works for you.
Cheers,
Damon Edis - interactivetools.com

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