简体   繁体   English

在PHP日历上显示事件

[英]Display Events on PHP Calendar

I would like to ask if I have a table of events on my database, eg 我想问一下我的数据库中是否有事件表,例如

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? 我已经创建了一个php日历,如何将上述事件显示在我的日历上,使其看起来像Google日历? eg on the for 2010-04-01, it will display "event1" on the month calendar. 例如,对于2010-04-01,它将在月份日历上显示“ event1”。

Thank you. 谢谢。

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: 假设您已经预先计算了一个用于显示日历网格的$ 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
}

Then while building the calendar, check if $day is present in the $events array and output the descriptions however you wish: 然后在构建日历时,检查$events数组中是否存在$ day,并根据$events输出描述:

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. 当然,如果您的活动跨越了多天,那么您就必须进行修改以解决该问题,但这至少可以让您从单日活动开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM