Show archive DESC from date and add link

Posted on 2012-03-31
Last Modified: 2012-08-14
I have an archive function working on my Blog using the following code...
mysql_select_db($database_acsinfo, $acsinfo);
 $query_archive = "SELECT YEAR(`eventdate`) AS 'year', MONTH(`eventdate`) AS 'month', COUNT(`id`) AS 'count'  FROM `latestnews` GROUP BY YEAR(`eventdate`), MONTH(`eventdate`) DESC
$result = mysql_query($query_archive, $acsinfo) or die(mysql_error());

$data = array();
while($row = mysql_fetch_assoc($result)) {
if( intval($row['count']) >0) $data[$row['year']][$row['month']] = $row['count'];


Open in new window

 <?php foreach ($data as $year => $months) {
    echo $year.'';
    foreach ($months as $month => $count) {
            echo date("F", mktime(0,0,0,$month,1,2000)).'('.$count.')<br>';
} ?>

Open in new window

How can I get the dates to show the other way round? ie 2012 first.

Also how would add a link? I need to send a link to the detail page which will show posts from that year & month.
Question by:BrighteyesDesign
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

OCDan earned 500 total points
ID: 37791495
Replace your archive query with this one:
SELECT YEAR(`eventdate`) AS 'year', MONTH(`eventdate`) AS 'month', COUNT(`id`) AS 'count'  FROM `latestnews` GROUP BY YEAR(`eventdate`), MONTH(`eventdate`) ORDER BY YEAR('eventdate') DESC, MONTH('eventdate') DESC

Author Comment

ID: 37796935
That's great, thanks for that.

Where would I add a link around the results so you click on  'July (1)' for example, it takes you a page with the month in the URL to display posts from that month?

I have tried...

        <a href="searchrecent.php?month=<?php echo $month ?>" class="sidemenu"> <?php foreach ($data as $year => $months) {
    echo $year.'';
    foreach ($months as $month => $count) {
            echo date("F", mktime(0,0,0,$month,1,2000)).'('.$count.')<br>';
} ?></a>

Open in new window


Author Comment

ID: 37805692
Anyone know how to do this?

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question