gpt4 book ai didi

php - 使用 PHP MySQL 创建议程 View - 按年份和月份划分获取的 MySQL 行

转载 作者:行者123 更新时间:2023-11-29 20:49:19 25 4
gpt4 key购买 nike

大家下午好。

我有一个像这样组织的 MySQL 表:

| ID | Date       | Some_Information           |
+----+------------+----------------------------+
| 1 | 2016-03-02 | A note about this day... |
| 2 | 2016-03-22 | A note about that day... |
| 3 | 2016-04-05 | Another note... |

我需要以类似的方式显示此数据Google Calendar app's agenda view 。为此,我应该:

  1. 将提取的已按日期、月份排序的行划分;
  2. 在显示每个月(假设是三月)对应的数据之前,显示 <div>以及相应月份的一些标题。

有人可以告诉我这是否可行,以及如何实现吗?

最佳答案

MySQL:

SELECT
table.ID,
table.date,
table.Some_Information,
MONTH(table.date) AS m,
YEAR(table.date) AS y
FROM table
ORDER BY table.date DESC

PHP:

$lastYm = '';
foreach($result as $row) {
$ym = $row['y'] . '-' . $row['m'];
if($ym != $lastYm) {
echo '<div class="month">' . $row['m'] . '</div>';
$lastYm = $ym;
}
echo '<div class="info">' . $row['Some_Information'] . '</div>';
}

关于php - 使用 PHP MySQL 创建议程 View - 按年份和月份划分获取的 MySQL 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38187625/

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