实现我的博客按年、按月的新闻,显示标题和创建数据
Archieve my blog'news by year and month, showing title and data of creation
我正在尝试存档新闻博客。
我想让它类似于所示 THERE
我现在可以显示年月和每个月的post个数,但是我不能显示标题和各种[=26=的slug ]s.
我的数据库结构是 table,其中包含以下行:
id (int), title(varchar), slug(varchar), created(timestamp).
你是代码:
$newsdata= $db->query("
SELECT
YEAR(created) AS YEAR,
MONTHNAME(created) AS MONTH,
title,
slug,
id,
COUNT(*) AS TOTAL
FROM pages
GROUP BY YEAR, MONTH
ORDER BY YEAR DESC, MONTH
")->fetchALL(PDO::FETCH_ASSOC);
$currentYear = null;
foreach($newsdata AS $news){
if ($currentYear != $news['YEAR']){
echo '<h2>'.$news['YEAR'].'<h2>';
$currentYear = $news['YEAR'];
}
echo '<p>' .$news['MONTH']. '</p><p>' .$news['TOTAL']. '</p>';
}
你可以看到结果HERE
我也知道有一个有趣的 post there 关于我的需求,但这并不能帮助我实现我的目标。
如果您想阅读整个讨论,我找到了正确的解决方案 HERE。
这是代码:
$db= new PDO('mysql:host=62.149.150.197;dbname=Sql692231_4', 'Sql692231', 'f7157ead'); //connection setup
$stmt = $db->query("
SELECT
Month(created) as Month,
Year(created) as Year,
title,
slug,
created
FROM pages
ORDER BY created DESC
");
// you will store current month here to control when the month changes
$currentMonth = 0;
// you will store current year here to control when the year changes
$currentYear = 0;
while($row = $stmt->fetch()){
// if the year changes you have to display another year
if($row['Year'] != $currentYear) {
// reinitialize current month
$currentMonth = 0;
// display the current year
echo "<li class=\"cl-year\">{$row['Year']}</li>";
// change the current year
$currentYear = $row['Year'];
}
// if the month changes you have to display another month
if($row['Month'] != $currentMonth) {
// display the current month
$monthName = date("F", mktime(0, 0, 0, $row['Month'], 10));
echo "<li class=\"cl-month\">$monthName</li>";
// change the current month
$currentMonth = $row['Month'];
}
// display posts within the current month
//$slug = 'a-'.$row['Month'].'-'.$row['Year'];
//echo "<li class=\"cl-posts\"><a href='$slug'>$monthName</a></li>";
echo '<li class="cl-posts active"><a href="http://www.matteocorona.com/cms_images/page.php?page='.$row['slug'].'">'.$row['title'].'</a></li>';
echo '<li class="cl-posts active">'.$row['created'].'</li>';
echo "<br/>";
}
结果,HERE
我正在尝试存档新闻博客。 我想让它类似于所示 THERE
我现在可以显示年月和每个月的post个数,但是我不能显示标题和各种[=26=的slug ]s.
我的数据库结构是 table,其中包含以下行: id (int), title(varchar), slug(varchar), created(timestamp).
你是代码:
$newsdata= $db->query("
SELECT
YEAR(created) AS YEAR,
MONTHNAME(created) AS MONTH,
title,
slug,
id,
COUNT(*) AS TOTAL
FROM pages
GROUP BY YEAR, MONTH
ORDER BY YEAR DESC, MONTH
")->fetchALL(PDO::FETCH_ASSOC);
$currentYear = null;
foreach($newsdata AS $news){
if ($currentYear != $news['YEAR']){
echo '<h2>'.$news['YEAR'].'<h2>';
$currentYear = $news['YEAR'];
}
echo '<p>' .$news['MONTH']. '</p><p>' .$news['TOTAL']. '</p>';
}
你可以看到结果HERE
我也知道有一个有趣的 post there 关于我的需求,但这并不能帮助我实现我的目标。
如果您想阅读整个讨论,我找到了正确的解决方案 HERE。
这是代码:
$db= new PDO('mysql:host=62.149.150.197;dbname=Sql692231_4', 'Sql692231', 'f7157ead'); //connection setup
$stmt = $db->query("
SELECT
Month(created) as Month,
Year(created) as Year,
title,
slug,
created
FROM pages
ORDER BY created DESC
");
// you will store current month here to control when the month changes
$currentMonth = 0;
// you will store current year here to control when the year changes
$currentYear = 0;
while($row = $stmt->fetch()){
// if the year changes you have to display another year
if($row['Year'] != $currentYear) {
// reinitialize current month
$currentMonth = 0;
// display the current year
echo "<li class=\"cl-year\">{$row['Year']}</li>";
// change the current year
$currentYear = $row['Year'];
}
// if the month changes you have to display another month
if($row['Month'] != $currentMonth) {
// display the current month
$monthName = date("F", mktime(0, 0, 0, $row['Month'], 10));
echo "<li class=\"cl-month\">$monthName</li>";
// change the current month
$currentMonth = $row['Month'];
}
// display posts within the current month
//$slug = 'a-'.$row['Month'].'-'.$row['Year'];
//echo "<li class=\"cl-posts\"><a href='$slug'>$monthName</a></li>";
echo '<li class="cl-posts active"><a href="http://www.matteocorona.com/cms_images/page.php?page='.$row['slug'].'">'.$row['title'].'</a></li>';
echo '<li class="cl-posts active">'.$row['created'].'</li>';
echo "<br/>";
}
结果,HERE