Trying to generate a weekly chart using PHP

I am coding some kind of graph and am stuck in iteration by dates.

I have a timeline generated within 5 business days, starting from 6:00 to 18:30. It's not a problem. I have a table with users and a table with dates. Users can have several tasks during the week.

This is an array:

Array ( [0] => Array ( [dateStart] => 2011-09-14 13:00:00 [dateEnd] => 2011-09-15 11:00:00 [eventType] => 1 [data] => test [taskDescription] => Vakantieverlof [taskColor] => ff6600 ) [1] => Array ( [dateStart] => 2011-09-14 15:00:00 [dateEnd] => 2011-09-14 18:00:00 [eventType] => 3 [data] => [taskDescription] => ADV [taskColor] => 336600 ) [2] => Array ( [dateStart] => 2011-09-15 16:00:00 [dateEnd] => 2011-09-16 10:00:00 [eventType] => 2 [data] => [taskDescription] => Ziek [taskColor] => ff0000 ) ) 

And this is a loop through the array:

 $dat=0; while($dat<count($row->dates)) { $color = "cccccc"; if(!empty($row->dates[$dat]['taskColor'])) { $color = $row->dates[$dat]['taskColor']; $desc = $row->dates[$dat]['taskDescription']; } else { $color = "cccccc"; } $datNext = $dat+1; if($datNext >= count($row->dates)) $datNext = $dat; if($row->dates[$datNext]['dateStart'] >= $hourCons AND $dat >= count($row->dates)) { $dat++; } else { if( $row->dates[$dat]['dateStart'] < $hourConsEnd AND $row->dates[$dat]['dateEnd'] > $hourCons ) { $wpcal .= "<div class=\"fullCell\" style=\"".$transparent." background-color: #".$color.";\"></div>"; } else { $wpcal .= "<div class=\"emptyCell\" style=\"\"></div>"; } } $dat++; } 

Now I get 3 timelines representing each scheduled task. eg:

Screen shot

But I want 1 timeline containing each of these tasks ... The green task in this case may overlap the orange.

Please help, I will do this a couple of days ...

+4
source share
3 answers

Instead of scrolling through an array of appointments, scroll through the hours of the day for each day. Inside this loop, check to see if any of the encounters match, and draw them if so.

 $filled_flag = false; foreach ($row->dates as $appt) { if ($appt['dateStart'] < $hourConsEnd && $appt['dateEnd'] > $hourCons) { $filled_flag = true; break; } } if ($filled_flag) { $wpcal .= "<div class=\"fullCell\" style=\"".$transparent." background-color: #".$color.";\"></div>"; } else { $wpcal .= "<div class=\"emptyCell\" style=\"\"></div>"; } 
0
source

First you need to combine the arrays into one array before you skip them, generating <div> s. For example, you can program an array with all the “slots”, and then fill them with the most suitable task. In foreach , where you are merging, you can apply some business logic, for example, if a task completely overlaps another, overwrite its slots, etc.

0
source

In the end, my array is part of the object for each user:

 stdClass Object ( [id] => 2 [username] => Emminet [user_level] => 100 [dates] => Array ( [0] => Array ( [dateStart] => 2011-09-07 07:00:00 [dateEnd] => 2011-09-14 15:29:59 [eventType] => 3 [data] => Dit is dan veld 2... [taskDescription] => ADV [taskColor] => 336600 ) [1] => Array ( [dateStart] => 2011-09-14 09:00:00 [dateEnd] => 2011-09-14 16:29:59 [eventType] => 3 [data] => dit is veld 1 [taskDescription] => ADV [taskColor] => 336600 ) [2] => Array ( [dateStart] => 2011-09-15 12:30:00 [dateEnd] => 2011-09-15 16:29:59 [eventType] => 3 [data] => [taskDescription] => ADV [taskColor] => 336600 ) ) ) 

So, I go through users and for each user I cycle in a week, like this:

 for($day=1; $day<=7; $day++){ // Timeframing settings $startDayAt = "06:00"; $frameAmount = 26; $timeFrame = 30; // minutes // Timeframing output for($i=0,$eTime = strtotime($startDayAt); $i < $frameAmount; $i++, $eTime = strtotime("+$timeFrame minutes", $eTime)) { $dat=0; while($dat<count($row->dates)) { $color = "cccccc"; if(!empty($row->dates[$dat]['taskColor'])) { $color = $row->dates[$dat]['taskColor']; $desc = $row->dates[$dat]['taskDescription']; } else { $color = "cccccc"; } $datNext = $dat+1; if($datNext >= count($row->dates)) $datNext = $dat; if($row->dates[$datNext]['dateStart'] >= $hourCons AND $dat >= count($row->dates)) { $dat++; } else { if( $row->dates[$dat]['dateStart'] < $hourConsEnd AND $row->dates[$dat]['dateEnd'] > $hourCons ) { $wpcal .= "<div class=\"fullCell\" style=\"".$transparent." background-color: #".$color.";\"></div>"; } else { $wpcal .= "<div class=\"emptyCell\" style=\"\"></div>"; } } $dat++; } } } 

So my actual cycle, in days, then hours and later users ...

0
source

All Articles