如果没有那个小时PHP的课程,如何隐藏表行

时间:2013-10-18 07:55:17

标签: php

我正在为wordpress的健身房制作一个网站,他们想要一个时间表插件,他们可以在这里添加他们的课程和时间。

我得到了时间表,它在正确的日期和时间显示正确的课程,但是他们还有另一个要求,当那个小时没有课程时,他们不想显示那个小时的表格行。这是一个例子:

        Monday   |   Tuesday  |    etc
9.00    
9.15    Fitness
9.30    
9.45   
10.00
10.15
10.45
11.00
11.15              Another lesson

在这个例子中,当它在10.00和11.00之间时没有课程。所以我只想显示9.00到9.45,用小时10隐藏所有行,然后在11.00再次开始,这样看起来像:

        Monday   |   Tuesday  |    etc
9.00    
9.15    Fitness
9.30    
9.45   
11.00
11.15              Another lesson

但是我不知道如何做到这一点,我编写了一段代码来收集时间表的数据,以及一段生成表格的代码。这是生成表格的代码:

 $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;

我添加了一个注释,我想我应该添加一个检查,检查是否必须显示表格行。

我希望有人可以帮助我!

谢谢!

编辑:

这就是我的数据阵列每天查找的方式:

Array
(
    [monday] => Array
    (
        [1382086800] => Array
            (
            )

        [1382087700] => Array
            (
            )

        [1382088600] => Array
            (
            Lesson Object
            )

数组中的每一天都包含当天的所有时间(12小时/ 15分钟)

2 个答案:

答案 0 :(得分:1)

尝试这样做(我希望这应该有用)

  

foreach($ arData ['times']为$ 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>';

答案 1 :(得分:1)

1)添加变量来存储包含任何课程的行

2)使用另一个变量临时存储行的HTML

3)如果有任何课程,请附加临时HTML。

详细修改

...
$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>';
...