timmy0320 Posted April 5, 2008 Share Posted April 5, 2008 I don't know why but I've been having some troubles the past couple days trying to figure this out. I'm trying to make a blog 'type' archive for news like so: My page is going to be somethings like archive.php?yr=2008?mon=04 When a current year or month is selected everything that doesn't relate to it will be collapsed. 2008 (3) January (1) 1 - New Years February (1) 14 - Valentines April (1) 1 - April Fools 2007 (2) December 25-Merry Christmas 1 - Welcome to the site I've been reading up on SQL syntaxs for this and I'm kind of sure that I got the syntax down (please correct me if I'm wrong) but I'm not quite sure on how to implement it into PHP if anyone can give me some help or ideas. Here is what I came up with: SELECT YEAR (date), MONTH(date), count(*) FROM posts GROUP BY YEAR(date), MONTH(date) ORDER BY date DESC I have really had no luck with finding help on this! Quote Link to comment Share on other sites More sharing options...
tippy_102 Posted April 5, 2008 Share Posted April 5, 2008 Someone solved this a few days ago. Perhaps their solution will help you: http://www.phpfreaks.com/forums/index.php/topic,190882.0.html Quote Link to comment Share on other sites More sharing options...
timmy0320 Posted April 6, 2008 Author Share Posted April 6, 2008 That one is a little different than mine. I will try to implement some of it but I'm trying to display mine like: 2008 [2] -April [2] --04 - testtt --01 - This will be the title of the post. This is what I have so far: <? $results = mysql_query("SELECT DISTINCT YEAR(date) AS `year`, DATE_FORMAT(date, '%m') AS `month`, DATE_FORMAT(date, '%d') AS `day`, count(id) as `posts`, post_title as `title` FROM blog_posts GROUP BY YEAR(date), DATE_FORMAT(date, '%d') ORDER BY date DESC", $connection); $posts = array(); while ($row = mysql_fetch_assoc($results)) { $post = $row['year']; if (!isset($posts[$post])) { $posts[$post] = array(); } $posts[$post][] = $row; } foreach ($posts as $year => $count) { echo '<strong>'.$year .' ['. count($count) .']</strong><br />'; foreach ($count as $row) { echo ' '.$row['month'].'['.count($row['month']).']<br />'; echo ' '.$row['day'].' - '.$row['title'].'<br />'; } } ?> My results are: 2008 [2] 04[1] 04 - testtt 04[1] 01 - This will be the title of the post. 1993 [2] 04[1] 14 - test 03[1] 15 - testing 1984 [1] 04[1] 09 - test I'm slowly getting there. Quote Link to comment Share on other sites More sharing options...
timmy0320 Posted April 6, 2008 Author Share Posted April 6, 2008 Alright, got it. Had to use a multi-dimensional array. Don't know why I post anymore since I always seem to figure it out before I get help If anyone ever needs help on tree displayed archives, just PM me. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.