开发者

Display Events on PHP Calendar

开发者 https://www.devze.com 2022-12-26 08:34 出处:网络
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

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 abov开发者_运维技巧e 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号