gpt4 book ai didi

php - 使用 PHP 和 MySQL 创建每月存档

转载 作者:行者123 更新时间:2023-11-29 03:04:52 25 4
gpt4 key购买 nike

我正在尝试在我的网站上创建一个新闻文件。我写了这段代码:

$sql_result = $db->query("
SELECT *,COUNT(id) AS itemCount
FROM post
GROUP BY DATE_FORMAT(date, '%Y-%m') DESC ");

while ( $row = $db->get_row( $sql_result ) ) {
$datetime = strtotime($row['date']);
$tday = date("Y-m", $datetime);
$count = $row['itemCount'];
echo "Month: {$tday} - News Number: {$count}<br>";
}

这是结果:

Month: 2013-06 - News Number: 4
Month: 2013-05 - News Number: 3
Month: 2013-04 - News Number: 4
Month: 2013-03 - News Number: 3

我的问题是,如何在该月之后的每个月中显示新闻标题?例如这样的事情:

Month: 2013-06 - News Number: 4
-news number 1 for this month
-news number 2 for this month
-news number 3 for this month
-news number 4 for this month
Month: 2013-05 - News Number: 3
-news number 1 for this month
-news number 2 for this month
-news number 3 for this month
Month: 2013-04 - News Number: 4
-news number 1 for this month
-news number 2 for this month
-news number 3 for this month
-news number 4 for this month
Month: 2013-03 - News Number: 3
-news number 1 for this month
-news number 2 for this month
-news number 3 for this month

最佳答案

尝试使用此代码。

 while ( $row = $db->get_row( $sql_result ) ) {
$datetime = strtotime($row['date']);
$tday = date("Y-m", $datetime);
$count = $row['itemCount'];
echo "Month: {$tday} - News Number: {$count}<br>";
$sql_result1 = $db->query("
SELECT newsTitle
FROM post
WHERE DATE_FORMAT(date, '%Y-%m')=DATE_FORMAT({$datetime}, '%Y-%m')");
while($row1 = $db->get_row( $sql_result1 ) ){
echo "News: {$row1['newsTitle']}";
}
}

关于php - 使用 PHP 和 MySQL 创建每月存档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17345204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com