title: Show year only once in archives loop
body: <p>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.</p>
  
  <p>I think the solution will be pretty easy but I'm so blinded that I just can't find the point on my own.</p>
  
  <p>This is my actual code and everything works fine, also the links:</p>
  
  <pre><code>$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 '&lt;dl&gt;';
  echo '&lt;dt&gt;Entries from &amp;nbsp;'. $get_year . '&lt;/dt&gt;';
  
  echo '&lt;dd&gt;&lt;a href="archives.php?month='. $get_month .'"&gt;Entries from &amp;nbsp;'. $this_month . '&amp;nbsp;&lt;/a&gt;(' . $entries . ')&lt;/dd&gt;';
  
  echo '&lt;/dl&gt;';  
  
  }
  </code></pre>
  
  <p>Ok. Then the result in the browser looks similar like this:</p>
  
  <ul>
  <li>Entries from  2012
  <ol>
  <li>Entries from  January (2)</li>
  </ol></li>
  <li>Entries from  2012
  <ol>
  <li>Entries from  February (1)</li>
  </ol></li>
  </ul>
  
  <p>Now my question: How can I resume all months inside the year? Like this:</p>
  
  <ul>
  <li>Entries from  2012
  <ol>
  <li>Entries from  January (2)</li>
  <li>Entries from  February (1)</li>
  </ol></li>
  </ul>
  
  <p>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.</p>
  
  <p>Can someone help me? Thanks!! </p>
  
  <p><em>---------------</em></p>
  
  <p>Here I am again to show the final (working!!) result of your friendly help:</p>
  
  <p>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.</p>
  
  <p>Sam's code worked just awesome... the only trouble I had was, that after using the array, it only printed out 'December' as months.</p>
  
  <p>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:</p>
  
  <pre><code>$this_month = date( 'F', mktime(0, 0, 0, $row['get_month']) );
  </code></pre>
  
  <p>Changing it (It's logic, but for me as greenhorn it toke me a while) to:</p>
  
  <pre><code>$this_month = date( 'F', mktime(0, 0, 0, $month['get_month']) );
  </code></pre>
  
  <p>Now everything work fine. Just how I expected. So this is the working final code:</p>
  
  <pre><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 =&gt; $months) {
    echo '&lt;dl&gt;';
    echo '&lt;dt&gt;Entries from &amp;nbsp;'. $year . '&lt;/dt&gt;';
  
  
      foreach($months as $month) {
      $this_month = date( 'F', mktime(0, 0, 0, $month['get_month']) );
  
    echo '&lt;dd&gt;&lt;a href="archives.php?month='. $month['get_month'] .'"&gt;Entries from &amp;nbsp;'. $this_month . '&amp;nbsp;&lt;/a&gt;(' . $month['entries'] . ')&lt;/dd&gt;';
  
    }
  
    echo '&lt;/dl&gt;';
  
  
  }
  </code></pre>
  
  <p>Thanks to all again! </p>