What is the best way to implement recurring events in FullCalendar through PHP / MySQL?

I am going to use jQuery-based FullCalendar in my online application using PHP / MySQL and noticed that when implementing repeating events, you should put a new element in the event array for each repetition (using the same ID), like this:

events: [ { id: 999, title: 'Repeating Event', start: new Date(y, m, d-1, 16, 0), allDay: false }, { id: 999, title: 'Repeating Event', start: new Date(y, m, d+6, 16, 0), allDay: false }, { id: 999, title: 'Repeating Event', start: new Date(y, m, d+13, 16, 0), allDay: false } ] 

Good, so it doesn’t matter. Using MySQL, I will simply feed the event, looping a bunch of times, for example, perhaps in the future, starting from the start date of this event, if it does not have an end date. But now I am loading a page with a bunch of JavaScript that might not even be needed (if the user simply opens the calendar to see the month). Not cool.

There must be a better way ... does anyone have their own experience?

+4
source share
1 answer

FullCalender will by default select events for the current time frame using lazyFetching. Typically, this means that you will only send an ajax call when the user disconnects the current month. You can reduce the load on the server by disabling caching:

 $('#calendar').fullCalendar({ events: { url: '/myfeed.php', cache: true } }); 

In addition, you should optimize your SQL on the server only to receive events for a certain period of time:

FullCalendar will determine the date range for which it needs events, and will pass this information in the GET parameters .... Here is the URL that FullCalendar can visit:

/myfeed.php?start=1262332800&end=1265011200& _ = 1263178646

If you use these get parameters in your SQL, you will drastically reduce the data you need to send to the client:

 SELECT * FROM events WHERE start > PARAMS_START_DATE AND end < PARAMS_END_DATE 

Obviously, this will not be so brief when using repeating events, you can, for example (in pseudocode):

 recurring_events.each do |recurring_event| running_date = recurring_event.starts_on while running_date < PARAMS_END_DATE if running_date > PARAMS_START_DATE events.push { :event => event.id, :date => running.date, ... } end running_date = running_date + recurring_event.frequency_as_days end end 

That way, you simply return the full calendar, which is applicable for the current view.

+8
source

All Articles