Reputation: 2660
Im making a website for a gym in wordpress, and they wanted a timetable plugin where they can add their lessons and times.
I got the timetable working, it displays the right lessons on the right day and time, however they also had another requirement, when there are no lessons for that hour, they do not want to display the table rows for that hour. Here's an example :
Monday | Tuesday | etc
9.00
9.15 Fitness
9.30
9.45
10.00
10.15
10.45
11.00
11.15 Another lesson
In this example, there are no lessons when its between 10.00 and 11.00 . So i only want to display 9.00 to 9.45 , hide all rows with the hour 10, and start again on 11.00 so it would look like :
Monday | Tuesday | etc
9.00
9.15 Fitness
9.30
9.45
11.00
11.15 Another lesson
However i have no idea on how to do this, i have written a piece of code that collects the data for the timetable, and a piece of code that generates the table. Here's the code that generates the table:
$table = '<table>';
$table .= '<thead>';
$table .= '<tr>';
$table .= '<th></th>';
foreach($this->arDays as $day => $id){
$table .= '<th>'.$day.'</th>';
// Display the days on top of the table.
}
$table .= '</tr>';
$table .= '</thead>';
$table .= '<tbody>';
foreach($arData['times'] as $time){
// I think here should be a check if we have to display the table rows,
// how ??
// Make a column with all the times
$table .= '<tr>';
$table .= '<td>'.date('H:i',$time).'</td>';
foreach($this->arDays as $day => $id){
// Then foreach time, foreach day check if there are lessons
$table .= '<td>';
// There is a lesson, display it in the timetable
if(!empty($arData[$day][$time])){
$arTimetable = $arData[$day][$time];
foreach($arTimetable as $oTimetable){
$table .= $oTimetable->oLesson->name;
}
}
$table .= '</td>';
}
$table .= '</tr>';
}
$table .= '</tbody>';
$table .= '</table>';
echo $table;
Ive added a comment on the place where i think i should add a check that checks if we have to display the table row.
I hope someone could help me out!
Thanks!!
Edit:
This is how my data array looks for each day :
Array
(
[monday] => Array
(
[1382086800] => Array
(
)
[1382087700] => Array
(
)
[1382088600] => Array
(
Lesson Object
)
Every day in the array contains all times for that day, (12 hours / 15 minutes)
Upvotes: 0
Views: 69
Reputation: 1950
1) add the variable to store the row contains any lessons or not
2) use another variable to temporary store the row's HTML
3) if any lesson exists, append the temp HTML.
detail modification
...
$table .= '<tbody>';
foreach($arData['times'] as $time){
$rowHasLesson = false; //(1)
//$table .= '<tr>'; //(2)
$tableRow = '<tr>'; //(2)
$tableRow .= '<td>'.date('H:i',$time).'</td>';
...
foreach($this->arDays as $day => $id){
...
if(!empty($arData[$day][$time])){
$rowHasLesson = true; //(1)
...
$tableRow .= 'YOUR DATA'; //(2)
}
//$table .= '</tr>';
$tableRow .= '</tr>';
if( $rowHasLesson ){ //(3)
$table .= $tableRow;
}
...
}
$table .= '</tbody>';
...
Upvotes: 1
Reputation: 1371
try do this (I hope this should work) inside
foreach($arData['times'] as $time)
:
$column = false;
// Make a column with all the times
foreach($this->arDays as $day => $id){
// Then foreach time, foreach day check if there are lessons
// There is a lesson, display it in the timetable
if(!empty($arData[$day][$time])){
if ($column === false){
$table .= '<tr>';
$table .= '<td>'.date('H:i',$time).'</td>';
$table .= '<td>';
$column = true;
} else {
$table .= '<td>';
}
$arTimetable = $arData[$day][$time];
foreach($arTimetable as $oTimetable){
if ($column) $table .= $oTimetable->oLesson->name;
}
}
if ($column) $table .= '</td>';
}
if ($column) table .= '</tr>';
Upvotes: 1