我需要帮助创建基于学年的帖子存档。在这个网站的帮助下,我设法创建了一个正常的存档。我仍然是 PHP 新手,感觉我错过了一些对普通后端开发人员来说可能很明显的东西。
这是我的存档当前外观的树形视图:
2013
April
Post Title
March
Post Title
February
Post Title
Post Title
Post Title
January
Post Title
Post Title
Post Title
2012
December
Post Title
Post Title
Post Title
November
Post Title
Post Title
Post Title
我不禁觉得它快到了,但我不知道如何让网站知道要显示哪些日期范围。我尝试过的一件事是添加此查询:
$result = mysql_query("SELECT * FROM table WHERE table_date BETWEEN '2012-08' and '2013-07'") or die(mysql_error());
它奏效了,这几乎就是我想要的……但我不想在结束之前为每个学年添加查询!必须有一种比每年手动添加这些范围更好的方法。
编辑:这是我当前的代码。它给了我错误“未定义的索引:hound_date”
$result = mysql_query("SELECT CONCAT( YEAR(hound_date-INTERVAL 7 MONTH),'-',
1+YEAR(hound_date-INTERVAL 7 MONTH)) AS 'academic year',
COUNT(*) AS 'number of events'
FROM hound
GROUP BY CONCAT( YEAR(hound_date-INTERVAL 7 MONTH),'-',
1+YEAR(hound_date-INTERVAL 7 MONTH))") or die(mysql_error());
// An array to store the data in a more managable order.
$data = array();
// Add each entry to the $data array, sorted by Year and Month
while($row = mysql_fetch_array($result))
{
$hound_date=my_date($row['hound_date']);
list($year,$month)=explode(" ", $hound_date);
$data[$year][$month][] = $row;
}
// Go through each Year and Month and print a list of entries, sorted by month.
foreach($data as $_year => $_months)
{
echo '<hr/>';
echo "<h2>{$_year}</h2>";
foreach($_months as $_month => $_entries)
{
echo '<hr/>';
echo "<h3>{$_month}</h3>";
echo "<ul>";
foreach($_entries as $_entry)
{
echo '<li><a href="/?story='.$_entry["hound_id"].'">'.$_entry["hound_name"].'</a> </li>';
}
echo "</ul>";
}
}
任何帮助将不胜感激!