Howdy,
I am currently in the process of writing my own news system in PHP/MySQL. I am currently trying to write a monthly archive system, but it isn't quite working yet. That's where you guys come in.
All the news will be stored in a table, called "news", with the following fields:
- id
- postdate
- title
- newstext
The "postdate" field contains a timestamp, formatted like this: 20040409163002. Now what I'm trying to do is build a list with links to the monthly archives (where archived news means: news older than 45 days). Right now, I have this:
Now, while this does work, the problem is that when for example I have three news items in February and one in January, it outputs:
February 2004
February 2004
February 2004
January 2004
While I really want it to output this:
February 2004
January 2004
So, how can I get the code to output every month only once?
I've already asked this over at the DevShed forums but no one there really seemed to know. Hopefully someone here will be able to help me out
Edit -- I realize MySQL's GORUP_CONCAT might be what I'm looking for, but that's in MySQL 4.1, and my host uses 4.0.18 so I'm afraid that's of no use to me.
I am currently in the process of writing my own news system in PHP/MySQL. I am currently trying to write a monthly archive system, but it isn't quite working yet. That's where you guys come in.
All the news will be stored in a table, called "news", with the following fields:
- id
- postdate
- title
- newstext
The "postdate" field contains a timestamp, formatted like this: 20040409163002. Now what I'm trying to do is build a list with links to the monthly archives (where archived news means: news older than 45 days). Right now, I have this:
PHP Code:
function build_archive_list($days)
{
$query = mysql_query("
SELECT postdate,
DATE_FORMAT(postdate, '%M') as month,
DATE_FORMAT(postdate, '%Y') as year
FROM news WHERE DATE_SUB(NOW(), INTERVAL $days DAY) >= postdate
ORDER BY postdate DESC
") or die ("Failed to get news dates for archive.");
while ($months = mysql_fetch_assoc($query))
{
$month = $months['month'];
$year = $months['year'];
echo "<span class=\"row\">" . $month . " " . $year . "</span><br />";
}
}
February 2004
February 2004
February 2004
January 2004
While I really want it to output this:
February 2004
January 2004
So, how can I get the code to output every month only once?
I've already asked this over at the DevShed forums but no one there really seemed to know. Hopefully someone here will be able to help me out
Edit -- I realize MySQL's GORUP_CONCAT might be what I'm looking for, but that's in MySQL 4.1, and my host uses 4.0.18 so I'm afraid that's of no use to me.
Comment