I want to create an archive list like this:
- 2014
- March
- Feb
- Jan
- Post 1
- Post 2
- 2013
- November
- Post 1
- November
I am using by PDO. 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 postTitle, Month(postDate) as Month, Year(postDate) as Year FROM blog_posts_seo ORDER BY postDate DESC"); while($row = $stmt->fetch()){ $posts = $row['postTitle']; $year = $row['Year']; $monthName = date("F", mktime(0, 0, 0, $row['Month'], 10)); $slug = 'a-'.$row['Month'].'-'.$row['Year']; echo "<li>$year</li>"; echo "<ul><li><a href='$slug'>$monthName</a></li>"; echo "<ul><li><a href='#'>$posts</a></li></ul></ul>"; } ?> </ul>
The result im getting is as follows:
2014 May Post 2013 June Post 2013 June Post 2012 June Post
In short how do I group the posts and months accordingly using php? 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!