acslater323
04-09-2007, 10:01 AM
I have a database-driven calendar page that pulls out records sorted by date and time for a user-specified date range. Easy enough. The catch, however, is that in the back end, I've added a check box to flag an event as recurring, with frequencies of Weekly, Bi-Weekly, and Monthly. I'd like to expand those options, but let's just leave it at that for now.
Here's how I'm tackling the problem right now to show all standard (non-recurring) events inline with the recurring events, in some basic pseudo code..
-Query the DB for all non-recurring events in specified date range.
-Build a 3D array of all these events like so:
Array (
[DATE] =>
Array (
[event1] =>
Array(
[title]
[time]
[description]
)
[event2] =>
etc....
)
[DATE] =>
Array (
etc...
)
-Now query the DB for all RECURRING events in the given range and loop through them, getting their frequencies. For each one, loop through the above array to find all the spots for these recurring events to go. For weekly events, for instance, the loop just keeps adding "+1 week" to the UNIX timestamp and creates new indexes in my 3D array.
-Finally, loop through the 3D array and show the calendar
Now.. all of this is very cumbersome, I know. We don't really like to work with 3D arrays if we don't have to. So someone made the suggestion that I should just loop a query for each day and find out if there are any events scheduled for that day, recurring or not.
My thinking is, it's never good to do a lot of DB queries in one script. For instance, if the user has set a one-year view of the calendar, that's a minimum of 365 queries in one script. That can't be good. Or does it really make more sense? It would certainly tighten up the code and make things easier, but isn't that a huge resource hog?
Any thoughts on these two options would be appreciated. Also open to any new perspectives. Can't be that uncommon a problem!
Here's how I'm tackling the problem right now to show all standard (non-recurring) events inline with the recurring events, in some basic pseudo code..
-Query the DB for all non-recurring events in specified date range.
-Build a 3D array of all these events like so:
Array (
[DATE] =>
Array (
[event1] =>
Array(
[title]
[time]
[description]
)
[event2] =>
etc....
)
[DATE] =>
Array (
etc...
)
-Now query the DB for all RECURRING events in the given range and loop through them, getting their frequencies. For each one, loop through the above array to find all the spots for these recurring events to go. For weekly events, for instance, the loop just keeps adding "+1 week" to the UNIX timestamp and creates new indexes in my 3D array.
-Finally, loop through the 3D array and show the calendar
Now.. all of this is very cumbersome, I know. We don't really like to work with 3D arrays if we don't have to. So someone made the suggestion that I should just loop a query for each day and find out if there are any events scheduled for that day, recurring or not.
My thinking is, it's never good to do a lot of DB queries in one script. For instance, if the user has set a one-year view of the calendar, that's a minimum of 365 queries in one script. That can't be good. Or does it really make more sense? It would certainly tighten up the code and make things easier, but isn't that a huge resource hog?
Any thoughts on these two options would be appreciated. Also open to any new perspectives. Can't be that uncommon a problem!