This one is a bit of a toughie. I've been wracking my brain over this for a couple of days now, and I haven't been able to come up with any result except for giving myself a headache. Ouch.
I'm trying to use PHP to develop a weekly (7 day) calendar which displays the weekly appointments in a tabular view, like you find in any personal organizer (palm pilot, outlook, etc). The thing is, I need to be able to list an indeterminate number of simultaneous events side by side on my calendar, and I need an algorithm that determines (for each day) the maximum number of columns needed to list all the simultaneous events and (for each event) the appropriate colspan for said event. <a href="
http://www.horde.org/kronolith/screenshots/workweek.png">Her
e's a screenshot</a> from Kronolith, which is doing exactly what I want (I don't want to mess with Kronolith for this application since I don't want all the overhead of the horde libraries. For my purposes, I just need a weekly calendar).
I have everything working fine (all the display stuff) except for the above algorithm. What I'm using now is kind of a kludge that will print incorrect output every once in a while. That's not ideal.
Here's how I have this structured: I have an associative array filled with all my events for the week (day of event along with starttime and endtime), which I can preprocess in any way I see fit. For each day, I need to be able to determine the total number of table columns needed to correctly display the information, as well as the number of columns each individual event will span. I am hardcoding my table to work in 15 minute increments, and you can assume that your algorithm wont have to deal with any messiness on that front (ie events will only start and end at 00, 15, 30, or 45).
The display requirements for each day are that 1.) the events for each day must fill up the entire day column at some point. For example, if you have one group of events situated to be three events across (giving a maxcolumn value of 3 for that day), and a single event later in the day, that event must be given a colspan of 3, to fill up the entire day column. However, if you have a three events simultaneously earlier in the day, and then two events simultaneously later, it's okay for one of those two events to have a colspan of 1 and the other a colspan of 2. Your algorithm should divvy up these colspans between the two in some manner. Also, keep in mind that there are several possible logic states that dictate whether or not a new column needs to be added for a particular day, and I need your algorithm to successfully deal with every combination. I'm going to run your algorithm once for every day of the week, so you only need to deal with the events for one day. You may assume that the array of events you're dealing with only contains events for the day you're working on.
Your algorithm can be done in pseudocode that just details the logic of what you're doing, but please be specific with what your arrays are up to so that it it's pretty clear what's going on. You basically need to populate some value with the max number of columns I need to print for that day, as well as populate the event array (which contains each event) with the colspan I will need to display each of them at in my table. You can take as many processing loops as you need to come up with these values.
Thanks in advance for considering this. I know it's a bear of a problem, which will probably take some considerable figuring. I will consider adding more points if it becomes clear that several people have contributed to painting a clearer picture for me. Let me know if you have any questions!
Start Free Trial