poochiteap 0 Newbie Poster

want to create an archive list like this:

2014
    -March
    -Feb
    -Jan
        *Post 1
        *Post 2
2013
    -November
        *Post 1

My config file is:

<?php
ob_start();
session_start();

//database credentials
define('DBHOST','127.0.0.1');
define('DBUSER','name');
define('DBPASS','pass');
define('DBNAME','sample');

$db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


//set timezone
date_default_timezone_set('Europe/London');
?>`

the table I m using is having postDate as Datetime. postSlug is used to get a clean url. The coding I am using now is:

<h1>Archives</h1>
<hr />

<ul>
<?php
$stmt = $db->query("SELECT Month(postDate) as Month, Year(postDate) as Year,
                           postTitle, postSlug 
                    FROM blog_posts_seo 
                    GROUP BY Month(postDate), Year(postDate) 
                    ORDER BY postDate DESC");

while($row = $stmt->fetch()){
    $monthName = date("F", mktime(0, 0, 0, $row['Month'], 10));
    $slug = 'a-'.$row['Month'].'-'.$row['Year'];
    echo "<li><a href='$slug'>$monthName</a></li>";
    echo '<li class="active"><a href="c-'.$row['postSlug'].'">'.$row['postTitle'].'</a></li>';
}
?>
</ul>

The result im getting is without year but as follows:

May
Post first out 4
April
Post first out of 3

I am am beginner in php and mysql. Therefore it would be of great help if you can help me in the complete coding if you know the solution. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.