HTML时间表与混乱的HTML单元格


php HTML timetable with messed up html cells

我想我在创建学校时间表时完全搞砸了html表我需要的是需要时间表但是我得到的是这样的时间表,错误的时间表

My Code is

// 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>";

有谁能看看吗,因为我一直在撞我的头。另外,空单元格也可以显示为"未分配"

我搜了一下

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