php HTML时间表
我的第一篇文章是这样写的。我正在开发一个基于php的时间表模块。看来我90%的代码都运行良好。只是无法正确显示循环,以时间表格式显示我的数据 我希望我的时间表与此类似: 我的代码:php HTML时间表,php,html,Php,Html,我的第一篇文章是这样写的。我正在开发一个基于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_tim
//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>";
//星期一从1开始的天数数组
$days=数组('1','2','3','4','5','6','7');
//从讲座中选择所有时间
$hours=选择id,从讲座开始的时间;
$tachedule=从时间表中选择tachedule.id、tachedule.day、tachedule.讲座id、tachedule.subject\u id;
回声“;
回声“;
//回声“;
//呼应“日”;
//回声“;
foreach(小时为$hh)
{
回声“;
{echo$hh->开始时间;}
回声“;
}
回声“;
foreach(小时为$hour)
{
foreach($天作为$天)
{
回声“;
foreach(时间表为$tt)
{
回声“;
如果($tt->day==$day和$tt->讲座id==$hour->id)
{echo$tt->subject_id;}
回声“;
}
回声“;
}
}
回声“;
我的输出如下。这没关系,只是我不能把天放在这一行的开头。有人能帮我解决这个小问题吗。我想我搞砸了HTML
您可能需要在第一行中显示开始时间的空单元格,然后对于包含受试者的每一行,添加一个显示一周中某一天的单元格 另外,我觉得如果在SQL中使用ORDERBY子句,您可能会将三重for循环减少到至少两倍for循环。可能按天订购,然后按开始时间订购(对于开始时间,您需要加入到讲课表中)。看起来外循环应该在几天内迭代,内循环应该在开始时间内迭代 编辑:你甚至可以有一个循环,如果你要交叉连接不同的日期和开始时间,按天排序,然后按开始时间排序,然后根据你的实际时间表进行左外连接。您将在每一行中得到空值,这些空值对应于没有预定讲座的日期和时间。您还将知道,您的查询每天提供相同数量的行。这样,您只需每隔N行创建一个新的
,其中N是一天中的开始次数
EDIT2:关于循环嵌套,我的意思是:
`
foreach($days作为$day)
{
echo”“;//每天都是一个表行
echo“.getDayNameByNumber($day)。”“//使用此处建议的方法之一,即索引到名称数组中,如果PHP有内置函数,则使用该函数(更好,因为它可能遵循系统的区域设置并为您翻译…)
foreach(小时为$hour)
{
//一天中的每一个小时都是一排细胞
//优化这个循环。。。
foreach(时间表为$tt)
{
回声“;
如果($tt->day==$day和$tt->讲座id==$hour->id)
{echo$tt->subject_id;}
回声“;
}
回声“;
}
}
您可能需要在第一行中显示开始时间的空单元格,然后对于包含主题的每一行,添加一个显示星期几的单元格
另外,我觉得如果在SQL中使用ORDERBY子句,您可能会将三重for循环减少到至少两倍for循环。可能按天订购,然后按开始时间订购(对于开始时间,您需要加入到讲课表中)。看起来外循环应该在几天内迭代,内循环应该在开始时间内迭代
编辑:你甚至可以有一个循环,如果你要交叉连接不同的日期和开始时间,按天排序,然后按开始时间排序,然后根据你的实际时间表进行左外连接。您将在每一行中得到空值,这些空值对应于没有预定讲座的日期和时间。您还将知道,您的查询每天提供相同数量的行。这样,您只需每隔N行创建一个新的
,其中N是一天中的开始次数
EDIT2:关于循环嵌套,我的意思是:
`
foreach($days作为$day)
{
echo”“;//每天都是一个表行
echo“.getDayNameByNumber($day)。”“//使用此处建议的方法之一,即索引到名称数组中,如果PHP有内置函数,则使用该函数(更好,因为它可能遵循系统的区域设置并为您翻译…)
foreach(小时为$hour)
{
//一天中的每一个小时都是一排细胞
//优化这个循环。。。
foreach(时间表为$tt)
{
回声“;
如果($tt->day==$day和$tt->讲座id==$hour->id)
{echo$tt->subject_id;}
回声“;
}
回声“;
}
}
首先,确保调用了正确的MySQL表字段
如果是,则将$days数组更改为具有周天数名称的双向数组,在循环中添加day单元格,并在顶部添加空单元格。(参见代码注释)
如果不想创建双向数组,请使用PHP函数。以下是一个例子:
<?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>';
}
?>
但是,出于性能原因,我建议您使用双向阵列。首先,确保您调用的是正确的MySQL
<?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
//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>';