在 PHP 日历上显示事件

发布于 2024-08-28 00:35:03 字数 361 浏览 3 评论 0原文

我想问我的数据库中是否有一个事件表,例如

description | startdt             | enddt
event1      | 2010-04-01 10:00:00 | 2010-04-01 13:00:00
event2      | 2010-04-09 14:00:00 | 2010-04-09 18:00:00
event3      | 2010-04-30 11:00:00 | 2010-05-02 16:00:00

我已经创建了一个php日历,我如何将上述事件显示到我的日历上,以便它看起来像谷歌日历?例如,对于 2010-04-01,它将在月历上显示“event1”。

谢谢。

I would like to ask if I have a table of events on my database, e.g.

description | startdt             | enddt
event1      | 2010-04-01 10:00:00 | 2010-04-01 13:00:00
event2      | 2010-04-09 14:00:00 | 2010-04-09 18:00:00
event3      | 2010-04-30 11:00:00 | 2010-05-02 16:00:00

I have already created a php calendar, how can I display these above events onto my calendar, so that it will look something like the google calendar? e.g. on the for 2010-04-01, it will display "event1" on the month calendar.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蘸点软妹酱 2024-09-04 00:35:03

假设您已经预先计算了 $year 和 $month 变量来显示日历网格,您可以从数据库中获取数据,如下所示:

$query = <<<EOF
SELECT DAY(startdt) AS day, description
FROM table
WHERE (YEAR(startdt) = $year) AND (MONTH(startdt) = $month)
EOF;

$stmt = mysql_query($query) or die ('Query error: ' . mysql_error());

$events = array();
while($row = mysql_fetch_assoc($stmt)) {
   $events[$row['day']][] = $row['description']; // might have multiple events on one day, so store as an array of events
}

然后在构建日历时,检查 中是否存在 $day $events 数组并按照您希望的方式输出描述:

for ($day = 1; $day <= 31; $day++) {
    if(isset($events[$day])) {
         ... display event descriptions
    }
}

当然,如果您有跨越多天的事件,那么您必须修改一些内容来处理该问题,但这应该让您至少从单一开始 -日活动。

Assuming you've got a $year and $month variable already precomputed for displaying the calendar grid, you'd fetch the data from the database something like this:

$query = <<<EOF
SELECT DAY(startdt) AS day, description
FROM table
WHERE (YEAR(startdt) = $year) AND (MONTH(startdt) = $month)
EOF;

$stmt = mysql_query($query) or die ('Query error: ' . mysql_error());

$events = array();
while($row = mysql_fetch_assoc($stmt)) {
   $events[$row['day']][] = $row['description']; // might have multiple events on one day, so store as an array of events
}

Then while building the calendar, check if $day is present in the $events array and output the descriptions however you wish:

for ($day = 1; $day <= 31; $day++) {
    if(isset($events[$day])) {
         ... display event descriptions
    }
}

Of course, if you have events that span multiple days, then you'll have to modify things to handle that, but this should get you started with at least single-day events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文