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: [ross] List Filtering Question

By greatjakes - June 19, 2009

Hi Ross,
I'm having difficulty getting the years to appear on the left side. Tried putting below code, but it doesn't list all the years -
<?php foreach ($newsRecords as $record): ?>
<?php echo date("Y", strtotime($record['date'])) ?>
<?php endforeach; ?>

Can't seem to figure out what code would make the list to appear correctl. I would appreciate it if you can help me figure out how to get this to work, Thanks.

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/