Jump to content

Trying to create blog like archive


gijew

Recommended Posts

I'm working on a journal application that creates a sidebar similiar to that of a blog that should group by month and year.

 

Perhaps I'm too tired or perhaps I've been working too much today but this one is beyond me right now. I'm getting weird results and this looks like it should work for me. Anyhoo, I'm using AdoDb to pull out the data so just an FYI ;)

 

From what I know I know that this should be grouping the years and the months correctly but as you can see from the results it isn't. It actually looks like I'm outputting the first row successfully and the rest of the data is coming out as a piece of an array...

 

Thanks for any help you can provide me!

 

Database field `created_on`
Data example: 2008-04-03 01:01:01
              2008-04-01 12:13:14
              2008-01-14 03:04:05
              2007-12-25 12:12:12

 

<?php
// PHP Function
Function getArchives () {
  global $db;
  $SQL = $db->GetAll("SELECT DISTINCT YEAR (created_on) AS `year`, MONTH(created_on) AS `month` FROM journal_categories GROUP BY YEAR(created_on), MONTH(created_on) ORDER BY created_on DESC");
  If (Count($SQL) != 0) {
    ForEach ($SQL AS $REC) {
      $O .= $REC['month'].'-'.$REC['year'].'<br />';
    }
  }
  return $O;
}
?>

 

::Output::
4-2008
4-8
1-8

Link to comment
https://forums.phpfreaks.com/topic/99491-trying-to-create-blog-like-archive/
Share on other sites

GOT IT thanks to help from someone else!!!! Just thought to post a solution if anyone ever stumbles on this in the future.

 

It's all in the query!

 

mysql_query("SELECT DISTINCT DATE_FORMAT(created_on,'%m-%Y') as dates FROM journal_categories GROUP BY created_on ORDER BY created_on DESC");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.