简体   繁体   English

php HTML 时间表与乱七八糟的 html 单元格

[英]php HTML timetable with messed up html cells

I think I have completely messed up html table while creating a school timetable what i require is Required Timetable But I am getting timetable like this Incorrect Timetable我想我在创建学校时间表时完全弄乱了 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>";

Can anyone please look into it as I have been banging my head.任何人都可以看看它,因为我一直在敲我的头。 Also it would be great that empty cells will be shown as "Unassigned"空单元格将显示为“未分配”也很棒

Did it after some googling做了一些谷歌搜索后

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM