Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7442495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:10:59+00:00 2026-05-29T11:10:59+00:00

I just started with PHP and mySQL and I’m creating some kind of blog.

  • 0

I just started with PHP and mySQL and I’m creating some kind of blog. At time I think it’s going well – I just got some trouble with my archives code.

I think the solution will be pretty easy but I’m so blinded that I just can’t find the point on my own.

This is my actual code and everything works fine, also the links:

$sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*) AS entries FROM blogdata GROUP BY get_month ORDER BY date ASC") or die('No response from database.');


while ($row = mysql_fetch_array($sql)) {

$get_year = $row["get_year"];
$get_month = $row["get_month"];
$entries = $row["entries"];

    // get month name
$this_month = date( 'F', mktime(0, 0, 0, $row["get_month"]) );



echo '<dl>';
echo '<dt>Entries from &nbsp;'. $get_year . '</dt>';

echo '<dd><a href="archives.php?month='. $get_month .'">Entries from &nbsp;'. $this_month . '&nbsp;</a>(' . $entries . ')</dd>';

echo '</dl>';  

}

Ok. Then the result in the browser looks similar like this:

  • Entries from 2012
    1. Entries from January (2)
  • Entries from 2012
    1. Entries from February (1)

Now my question: How can I resume all months inside the year? Like this:

  • Entries from 2012
    1. Entries from January (2)
    2. Entries from February (1)

I’m scared about creating a 12-months array cause maybe not every month there will be entries, and I don’t want to show empty months.

Can someone help me? Thanks!!

—————

Here I am again to show the final (working!!) result of your friendly help:

At least, I used Sam’s version cause it was just for what i was looking for. I really appreciate the other answers – specially ’cause now I have more stuff to think about for the next tries.

Sam’s code worked just awesome… the only trouble I had was, that after using the array, it only printed out ‘December’ as months.

So I looked over again the code and after 2 hours of seek and try, I found the point of trouble. It was nested in the following line:

$this_month = date( 'F', mktime(0, 0, 0, $row['get_month']) );

Changing it (It’s logic, but for me as greenhorn it toke me a while) to:

$this_month = date( 'F', mktime(0, 0, 0, $month['get_month']) );

Now everything work fine. Just how I expected. So this is the working final code:

    $sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*) AS entries FROM blogdata GROUP BY get_year, get_month ORDER BY date ASC") or die('No response from database.');


    $entries = array();
    while ($row = mysql_fetch_assoc($sql)) {
    $entries[$row['get_year']][] = $row;
}

    foreach($entries as $year => $months) {
  echo '<dl>';
  echo '<dt>Entries from &nbsp;'. $year . '</dt>';


    foreach($months as $month) {
    $this_month = date( 'F', mktime(0, 0, 0, $month['get_month']) );

  echo '<dd><a href="archives.php?month='. $month['get_month'] .'">Entries from &nbsp;'. $this_month . '&nbsp;</a>(' . $month['entries'] . ')</dd>';

  }

  echo '</dl>';


}

Thanks to all again!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:10:59+00:00Added an answer on May 29, 2026 at 11:10 am

    I find the simplest way is something along these lines:

    $entries = array();
    while ($row = mysql_fetch_assoc($sql)) {
      $entries[$row['get_year']][] = $row;
    }
    
    foreach($entries as $year => $months) {
      echo '<dl>';
      echo '<dt>Entries from &nbsp;'. $year . '</dt>';
    
      echo '<dd>';
      foreach($months as $month) {\
        $this_month = date( 'F', mktime(0, 0, 0, $row["get_month"]) );
    
        echo '<a href="archives.php?month='. $month['get_month'] .'">Entries from &nbsp;'. $this_month . '&nbsp;</a>(' . $month['entries'] . ')';
      }
      echo '</dd></dl>';
    
    
    }
    

    That HTML markup isn’t ideal, but you should get the idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started using mysqli API for PHP. Apparently, every time an object of
I have just started out with testing some php mvc framework In it, it
I have just started PHP and MySQL and have created a login form and
I have just started PHP and mySQL and need to know if this is
I have just started learning MySQL and am having trouble extracting matching flights from
I have just started a new PHP project and I was thinking it's time
Well I just started using this new php PDO, and am having this issue.
I've just started using MySQL with PHP and I'd like to know if it's
I'm an iPhone developer, and I've just started out with PHP & mysql (making
So I'm really really really new to PHP and MySQL (just started today!). I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.