繁体   English   中英

php HTML时间表

[英]php HTML timetable

我关于SO的第一篇文章。 我正在研究基于php的时间表模块。 看来我90%的代码都能正常工作。 只是无法正确显示循环以时间表格式显示我的数据。

我希望自己的时间表像这样的表:

所需时间表

我的代码:

//Array for days starting from 1 for monday
$days = array('1','2','3','4','5','6','7');

//Selecting all the hours from lectures
$hours = select id, start_time from lectures;

$timetable = select timetable.id, timetable.day, timetable.lecture_id, timetable.subject_id from timetable;

        echo "<table>";
        echo "<tr>";
            //echo "<td>";
            //  echo "Day";
            //echo "</td>";
    foreach($hours as $hh)
                        {
                        echo "<td>";
                        {echo $hh->start_time;}
                        echo "</td>";   
                        }
    echo "</tr>";                   
    foreach($hours as $hour)
        {
            foreach($days as $day)
                {
                    echo "<tr>";
                        foreach($timetable as $tt)
                        {
                            echo "<td>";
                            if ($tt->day==$day AND $tt->lecture_id==$hour->id)
                            {echo $tt->subject_id;}
                            echo "</td>";
                        }
                    echo "</tr>";   
                }
        }
    echo "</table>";

我的输出如下。 可以,但是我无法将Days放在行的开头。 任何人都可以帮我解决这个小问题。 我想我搞砸了HTML

当前时刻表

您可能需要在第一行中显示开始时间的空白单元格,然后为包含主题的每一行添加一个显示星期几的单元格。

另外,我觉得如果在SQL中使用ORDER BY子句,则可能会将三重for循环减少到至少双倍for循环。 也许按天排序,然后按开始时间排序(对于开始时间,您需要与讲课表连接)。 似乎外循环应该迭代数天,而内循环则要迭代开始时间。

编辑:如果您要交叉连接不同的日期和开始时间,甚至可以只有一个循环,按天数然后按开始时间排序,然后使用实际时间表进行左外部连接。 您最终将在每一行中都有NULL,这些NULL与没有安排的讲座的日期和时间相对应。 您还将知道查询每天提供相同数量的行。 这样,您可以每N行创建一个新的<tr /> ,其中N是一天中的开始时间。

EDIT2:关于循环嵌套,我的意思是这样的:

foreach($days as $day)
    {
        echo "<tr>"; // Every day is a table row
        echo "<td>" . getDayNameByNumber($day) . "</td>" // use one of the methods being suggested here, i.e. indexing into an array of names, a built-in function if PHP has it (would be better, because it would likely follow the system's locale and translate it for you...)
        foreach($hours as $hour)
            {
                    // Every hour in a day is a cell in the row
                    // optimize this loop...
                    foreach($timetable as $tt)
                    {
                        echo "<td>";
                        if ($tt->day==$day AND $tt->lecture_id==$hour->id)
                        {echo $tt->subject_id;}
                        echo "</td>";
                    }
                echo "</tr>";   
            }
    }

首先,请确保您要调用正确的MySQL表字段。

如果是,则将$ days数组更改为带有工作日名称的双向数组,在循环中添加day单元格,在顶部添加一个空单元格。 (请参见代码注释)

<?php

// 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 = select id, start_time from lectures;

// $timetable = select timetable.id, timetable.day, timetable.lecture_id, timetable.subject_id from timetable;



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

?>

如果您不想创建双向数组,请使用PHP的函数jddayofweek() 这是一个例子:

<?php


//Array for days starting from 1 for monday
$days = array( 1, 2, 3, 4, 5, 6, 7 );

foreach ( $days as $day ) {

    echo jddayofweek( $day - 1, 1 ), '<br>';

}

?>

但是,出于性能原因,我建议您使用双向阵列。

谷歌搜索后是否做到了

$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