-1
我在我的网站上创建了一个档案导航,并找到了一些关于如何做到这一点的文章,并将一些代码放在一起,但似乎无法使其工作。博客档案
我希望它显示,如:
January 2015 (9)
December 2014 (8)
November 2014 (10)
October 2014 (15)
...
下面的代码:
$sql = "SELECT YEAR(timestamp_published) AS 'year', Month(timestamp_published) AS 'month', COUNT(id) AS 'count' FROM table GROUP BY YEAR(timestamp_published), MONTH(timestamp_published) DESC";
$data = $db->query($sql);
while($row = $db->fetch_assoc()) {
$data[$row['year']][$row['month']] = $row['count'];
}
foreach ($data as $year => $months) {
echo archive_date($month).' ';
foreach ($months as $month => $count) {
echo $year.' ('.$count.') (' . $data->id . ')<br/>';
}
}
}
谢谢!我最终解决了我自己的问题。我是在一堂课中建立这个课题的,有几个问题。我现在已经有了代码工作,在这里它适用于将来的任何人。
$sql = "SELECT YEAR(timestamp_published) AS 'year', MONTH(timestamp_published) AS 'month', COUNT(`publish`) AS 'count' FROM ".static::$table_name." GROUP BY YEAR(timestamp_published) DESC, MONTH(timestamp_published) ASC";
$result = $db->query($sql);
$data = array();
while($row = $result->fetch_assoc()) {
$data[$row['year']][$row['month']] = $row['count'];
}
foreach ($data as $year => $months) {
echo '<strong>'.$year.'</strong><br/><br/>';
foreach ($months as $month => $count) {
$dateObj = DateTime::createFromFormat('!m', $month);
$monthName = $dateObj->format('F'); // March
echo $monthName . ' ('.$count.')<br/>';
}
echo '<hr/>';
}
实际上,它找出来是这样的:
2015年
一月(3)
二月(5)
三月(6)
...
January(7)
February(9)
...
是什么您提供的代码的输出?任何错误? – 2015-04-02 07:28:17