doukang7858 2010-04-08 18:16 采纳率: 0%
浏览 62
已采纳

在PHP日历上显示事件

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.

  • 写回答

1条回答 默认 最新

  • doule0941 2010-04-09 03:05
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试