php HTML时间表,搞乱了html单元格

时间:2015-10-19 18:25:01

标签: php html

我认为在创建学校时间表时我完全搞砸了html表 我需要的是 Required Timetable 但我正在制定像Incorrect Timetable

这样的时间表

我的代码是

// days of week array
$days = array( 
1 => 'Monday', 
2 => 'Tuesday', 
3 => 'Wednesday', 
4 => 'Thursday', 
5 => 'Friday', 
6 => 'Saturday', 
7 => 'Sunday' );

//Selecting all the hours from lectures
 $hours = DB::table('timetablelectures')->select('timetablelectures.id', 'timetablelectures.start_time')
                                ->get();

$timetable = DB::table('timetable')->select('timetable.id', 'timetable.day', 'timetable.lecture_id', 'timetable.subject_id')
                                ->get();

echo "<table>";
echo "<tr>";

echo '<td></td>'; // empty cell

foreach( $hours as $hh ) {

    echo "<td>";    
    echo $hh->start_time;
    echo "</td>";

}

echo "</tr>";

foreach( $hours as $hour ) {

    foreach( $days as $day => $day_name ) {

        echo "<tr>";
        echo '<td>', $day_name, '</td>'; // day of the week

        foreach( $timetable as $tt ) {

            echo "<td>";

            if( (int)$tt->day == $day and $tt->lecture_id == $hour->id ) {
                echo $tt->subject_id;
            }

            echo "</td>";
        }

        echo "</tr>";
    }
}
echo "</table>";

任何人都可以请一下,因为我一直在敲打我的脑袋。此外,空单元格将显示为“未分配”

1 个答案:

答案 0 :(得分:0)

经过一些谷歌搜索

$days = array("1"=>"Monday", "2"=>"Tuesday", "3"=>"Wednesday", "4"=>"Thursday", "5"=>"Friday", "6"=>"Saturday", "7"=>"Sunday", );

$hours = DB::table('timetablelectures')->select('timetablelectures.id', 'timetablelectures.start_time')
                                ->get();
$timetable = DB::table('timetable')->select('timetable.id', 'timetable.day', 'timetable.lecture_id', 'timetable.subject_id')
                                ->get();
//SET UP SCHEDULE    
$schedule = array();

    foreach($timetable as $tt) 
        {
        foreach($days as $x => $x_value) 
            {
                if ( $tt->day==$x)
                            {
                            $schedule["{$x}"][$tt->lecture_id][] = $tt->subject_id;
                            }
            }
        }


// DISPLAY SCHEDULE, headers first
?>

<table border="1">
    <tr><td align="center">Time</td>
 <?php foreach ( $hours as $hour ) 
                {
                    echo "<td>  $hour->start_time </td>";
                }
                echo "</tr>";

foreach ( $days as $x => $x_value) {
    echo "<tr><td>$x_value</td>";

    // roll through days
    foreach ( $hours as $hour ) {
        echo '<td>';
        // check for subjects in this slot
        if ( isset($schedule[$x][$hour->id]) ) {
            // and display each
            foreach ( $schedule[$x][$hour->id] as $subject ) {
                echo "$subject<br>";
            }
        }
        echo '</td>';
    }
    echo '</tr>';
}
echo '</table>';