php - Method for storing/displaying repeating weekly schedule -
i store , retrieve dynamic weekly schedule not @ dependent on actual date.
the data stored in mysql table (not ordered time):
(class , instructor columns store reference ids of other tables i've used actual names here make easier read @ glance)
---------------------------------------------------------------------- | id | time | dayofweek | class | instructor | ---------------------------------------------------------------------- | 1 | 6:30a | 1 | zumba | julie | ---------------------------------------------------------------------- | 2 | 9:00a | 3 | kickbox | devon | ---------------------------------------------------------------------- | 3 | 11:00a | 4 | zumba | alex | ---------------------------------------------------------------------- | 4 | 6:30a | 4 | dance | karen | ---------------------------------------------------------------------- | 5 | 5:00p | 1 | r-bar | karen | ---------------------------------------------------------------------- | 6 | 5:00p | 6 | dance | karen | ---------------------------------------------------------------------- | 7 | 9:00a | 7 | kinder | julie |
the final output visually (ordered time):
--------------------------------------------------------- | sun | mon | tue | wed | thu | fri | sat | ------------------------------------------------------------------- | 6:30a | zumba | | | dance | | | | ------------------------------------------------------------------- | 9:00a | | |kickbox| | | |kinder | ------------------------------------------------------------------- | 11:30a | | | | zumba | | | | ------------------------------------------------------------------- | 5:00p | r-bar | | | | | dance | | -------------------------------------------------------------------
but can't wrap head around how accomplish efficiently. i've searched google hours today , have come across few posts might work it's never quite i'm looking for.
i started out thinking running separate query each of 7 days per time slot, through function or otherwise, that's sloppy , way many queries such simple task. 7 days (columns) show timeslots (rows) may added or removed anytime depending if there event @ time.
next looked storing in array , combining rows duplicate times process days 1 one. i'm not sure how dynamically though...
i found example , think pretty close need: php - merge duplicate array keys in multidimensional array
after said , done planning on making simple admin page user add or remove events. ideas?
$a=array(); $a[] = array( 'id'=>'1' ,'time'=>'6:30a' , 'dayofweek'=>'2' , 'class'=>'zumba'); $a[] = array( 'id'=>'2' ,'time'=>'6:40a' , 'dayofweek'=>'3' , 'class'=>'zumba'); $a[] = array( 'id'=>'2' ,'time'=>'6:20a' , 'dayofweek'=>'3' , 'class'=>'zumba'); $a[] = array( 'id'=>'2' ,'time'=>'1:20p' , 'dayofweek'=>'3' , 'class'=>'zumba'); $new_array=array(); foreach($a $k =>$v){ if(!array_key_exists($v['time'],$new_array)){ $new_array[$v['time']]=array("","","","","","","",""); unset($new_array[$v['time']][0]); } $new_array[$v['time']][$v['dayofweek']]=$v['class']; } function cmp($a, $b) { $a = preg_replace('{\:}', '', $a); $a = preg_replace('{a}', '', $a); $a = preg_replace('{(.*?)p}', '100$1', $a); $a = (int)$a; $b = preg_replace('{\:}', '', $b); $b = preg_replace('{a}', '', $b); $b = preg_replace('{(.*?)p}', '100$1', $b); $b = (int)$b; if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; } uksort($new_array, "cmp"); $weekmap = array( '','sun','mon','tue','wed','thu','fri','sat'); print_r($new_array); foreach($new_array $k =>$v){ echo $k.'::'; foreach($v $k1 =>$v1){ //echo $weekmap[$k1]; //echo '->'; if($v1==''){ echo 'null'; } echo $v1; echo '|'; } echo php_eol; }
output
array ( [6:20a] => array ( [1] => [2] => [3] => zumba [4] => [5] => [6] => [7] => ) [6:30a] => array ( [1] => [2] => zumba [3] => [4] => [5] => [6] => [7] => ) [6:40a] => array ( [1] => [2] => [3] => zumba [4] => [5] => [6] => [7] => ) [1:20p] => array ( [1] => [2] => [3] => zumba [4] => [5] => [6] => [7] => ) ) 6:20a::null|null|zumba|null|null|null|null| 6:30a::null|zumba|null|null|null|null|null| 6:40a::null|null|zumba|null|null|null|null| 1:20p::null|null|zumba|null|null|null|null|
http://sandbox.onlinephpfunctions.com/code/8da03b1833f58e7f60888cfcfb6e544cd3ff10ad
Comments
Post a Comment