我刚开始使用PHP和mySQL,我正在创建某种博客。当时我认为它进展顺利 - 我的档案代码遇到了一些问题。
我认为解决方案非常简单,但我是如此盲目,以至于我自己找不到这一点。
这是我的实际代码,一切正常,链接:
$sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*) AS entries FROM blogdata GROUP BY get_month ORDER BY date ASC") or die('No response from database.');
while ($row = mysql_fetch_array($sql)) {
$get_year = $row["get_year"];
$get_month = $row["get_month"];
$entries = $row["entries"];
// get month name
$this_month = date( 'F', mktime(0, 0, 0, $row["get_month"]) );
echo '<dl>';
echo '<dt>Entries from '. $get_year . '</dt>';
echo '<dd><a href="archives.php?month='. $get_month .'">Entries from '. $this_month . ' </a>(' . $entries . ')</dd>';
echo '</dl>';
}
确定。然后浏览器中的结果看起来像这样:
现在我的问题是:我怎样才能在一年内恢复所有月份?像这样:
我害怕创造一个12个月的阵列,因为可能不是每个月都会有参赛作品,而且我不想显示空白月份。
有人能帮助我吗?谢谢!!
---------------
在这里,我再次展示你友好帮助的最终(工作!!)结果:
至少,我使用Sam的版本因为它只是为了我想要的东西。我真的很感激其他的答案 - 特别是'因为现在我有更多的东西要考虑下一次尝试。
Sam的代码工作得非常棒......我唯一遇到的麻烦就是,使用数组后,它只打印出'December'作为月份。
所以我再次查看代码,经过2个小时的搜索和尝试,我发现了麻烦。它嵌套在以下行中:
$this_month = date( 'F', mktime(0, 0, 0, $row['get_month']) );
改变它(这是逻辑,但对于我来说,作为新手,它让我有一段时间)给:
$this_month = date( 'F', mktime(0, 0, 0, $month['get_month']) );
现在一切正常。就是我的期望。所以这是最终的工作代码:
$sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*) AS entries FROM blogdata GROUP BY get_year, get_month ORDER BY date ASC") or die('No response from database.');
$entries = array();
while ($row = mysql_fetch_assoc($sql)) {
$entries[$row['get_year']][] = $row;
}
foreach($entries as $year => $months) {
echo '<dl>';
echo '<dt>Entries from '. $year . '</dt>';
foreach($months as $month) {
$this_month = date( 'F', mktime(0, 0, 0, $month['get_month']) );
echo '<dd><a href="archives.php?month='. $month['get_month'] .'">Entries from '. $this_month . ' </a>(' . $month['entries'] . ')</dd>';
}
echo '</dl>';
}
再次感谢所有人!
答案 0 :(得分:2)
我发现最简单的方法就是这样:
$entries = array();
while ($row = mysql_fetch_assoc($sql)) {
$entries[$row['get_year']][] = $row;
}
foreach($entries as $year => $months) {
echo '<dl>';
echo '<dt>Entries from '. $year . '</dt>';
echo '<dd>';
foreach($months as $month) {\
$this_month = date( 'F', mktime(0, 0, 0, $row["get_month"]) );
echo '<a href="archives.php?month='. $month['get_month'] .'">Entries from '. $this_month . ' </a>(' . $month['entries'] . ')';
}
echo '</dd></dl>';
}
HTML标记不理想,但你应该明白这一点。
答案 1 :(得分:1)
首先将您的查询按年份和月份更改为分组,而不仅仅是月份:
$query = "SELECT
YEAR(date) AS get_year,
MONTH(date) AS get_month,
COUNT(*) AS entries
FROM blogdata
GROUP BY get_year, get_month
ORDER BY date ASC"
我不记得旧的mysql API,所以这里的代码是PDO(未经测试,但应该在修复潜在的拼写错误后工作)
$pdo = new PDO('mysql:host=localhost;dbname=db', 'user', 'password');
$stmt = $pdo->prepare($query);
$stmt->execute();
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
//group results by year *
$groupped = array();
foreach ($results as $record) {
$groupped[$record['get_year']] = $record;
}
//print results
echo "<dl>";
foreach ($groupped as $year => $monthsRecords) {
echo "<dt>$year</dt>";
foreach ($monthsRecords as $record) {
echo "<dd>{$record['get_month']} : {$record['entries']}</dd>";
}
}
echo "</dl>";
您还可以尝试使用PDO :: FETCH_GROUP来简化代码,如中所述 fetchAll method doc但我从未尝试过这个,所以我不会给你代码。
答案 2 :(得分:0)
试试这个
$start_year = date("Y")-10;
$end_year = date("Y");
for ($i=$start_year;$i<$end_year;$i++){
$sql = mysql_query ("SELECT YEAR(date) AS get_year, MONTH(date) AS get_month, COUNT(*) AS entries FROM blogdata where YEAR(date) = $i GROUP BY get_month ORDER BY date ASC"); or die('No response from database.');
//execute your query here. get the
$result = mysql_query($sql);
echo '<dl>';
echo '<dt>Entries from '. $i . '</dt>';
while ($row = mysql_fetch_array($result )) {
$get_year = $row["get_year"];
$get_month = $row["get_month"];
$entries = $row["entries"];
// get month name
$this_month = date( 'F', mktime(0, 0, 0, $row["get_month"]) );
echo '<dd><a href="archives.php?month='. $get_month .'">Entries from '. $this_month . ' </a>(' . $entries . ')</dd>';
}
echo '</dl>';
}