I'm creating an Archive like tree display (different from the one posted a few days ago on here). I have got it resolved but I cannot get my counts to display correctly and it is only displaying one result for that day.
<?php
$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_link as `link`, 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)) {
if (!isset($posts[$row['year']][$row['month']])) {
$posts[$row['year']][$row['month']] = array();
}
$posts[$row['year']][$row['month']][] = $row;
}
// display years
foreach ($posts as $year => $yelement) {
echo '<a href=archives.php?yr='.$year.'>'.$year .'</a> ['. count($yelement) .']<br />';
// display months
foreach ($yelement as $month => $melement) {
if (isset($_GET['yr']) && $_GET['yr'] == $year) {
echo ' <a href=archives.php?yr='.$year.'&mon='.$month.'>'. $month .'</a> ['. count($melement) .']<br />';
}
// display days
foreach ($melement as $row) {
if (isset($_GET['yr']) && isset($_GET['mon']) && $_GET['mon'] == $month && $_GET['yr'] == $year) {
echo ' <a href=archives.php?yr='.$year.'&mon='.$month.'&title='.$row['link'].'>'. $row['day'].'</a> - '.$row['title'].'<br />';
}
}
}
}
?>
I have 3 entries for today and it's only displaying the top one queried. It results as
2008 [1]
04 [1]
06 - Test post
When it should be resulting as:
2008 [3]
04 [3]
06 - Test post
06 - Test post 2
06 - Test post 3
The counts are wrong because of this also. I don't know if it's something with my query or something with the code doing this. I'm guessing it is something to do with the query.