Archives page - need unique heading from date field

5 posts by 2 authors in: Forums > CMS Builder
Last Post: August 9, 2009   (RSS)

By Deborah - August 7, 2009

I don't see a reference to this on the forum.

I'd like to display a heading on each archive page indicating the month and year of the records shown on that page. Example: "Archives for May 2009"

List viewer page displays archive records by month and year:

<?php // get list of unique months and years with articles
$query = "SELECT DATE_FORMAT(week_of, '%M %Y') as dateAndYear, YEAR(week_of) as year, MONTH(week_of) as month FROM cms_countywide_pdfs GROUP BY dateAndYear ORDER BY week_of"; $result = mysql_query($query) or die("MySQL Error: ". htmlspecialchars(mysql_error()) . "\n"); while ($record = mysql_fetch_assoc($result)): ?>
<li><a href="archives.php?date_year=<?php echo $record['year'] ?>&amp;date_month=<?php echo $record['month'] ?>"><?php echo $record['dateAndYear']; ?></a></li>

Clicking one of the links listed above goes to the archive page (list viewer), which displays the records for that month.

Now all I need is a heading (Month + Year).

Seems as if it should be simple, but I haven't figured it out. Does anyone have a suggestion?

Deborah

Re: [Deborah] Archives page - need unique heading from date field

By Chris - August 8, 2009

Hi Deborah,

Since you are passing the year and month in the query string to your archive list page, you can extract them from the query string:

<?php echo htmlspecialchars($_GET['date_month'])?>
<?php echo htmlspecialchars($_GET['date_year'])?>


Note that htmlspecialchars() is important for cross-site scripting security here.

Also note that the above code will fail if those two query string variables are not supplied. If your page is meant to be called without them sometimes, you'll need some "if" code in there to detect that case.

Hope this helps! Let us know how it's going. :)
All the best,
Chris

Re: [chris] Archives page - need unique heading from date field

By Deborah - August 8, 2009

Chris,

Thanks! That worked for me.

The date is therefore displayed as: 7 2009

Ideally, I'd like to see: July 2009, but I don't think that's possible. Unless you know of a way to do that, I'll be happy with the formatting as is.

I appreciate your help.
Deborah

Re: [chris] Archives page - need unique heading from date field

By Deborah - August 9, 2009

Chris,

You are brilliant! Thanks for that piece of code. It displays the month and year exactly as ordered!

Deborah