简体   繁体   English

php HTML时间表

[英]php HTML timetable

My first post on SO. 我关于SO的第一篇文章。 I am working on a php based timetable module. 我正在研究基于php的时间表模块。 It seems 90% of my code is working fine. 看来我90%的代码都能正常工作。 Just not able to display the loop properly for showing my data in timetable format. 只是无法正确显示循环以时间表格式显示我的数据。

I want my time table to be like this table: 我希望自己的时间表像这样的表:

Required Timetable 所需时间表

My Code: 我的代码:

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

My output is as below. 我的输出如下。 This is okay except I am unable to put Days in the start of the row. 可以,但是我无法将Days放在行的开头。 Can anybody help me with this small issue. 任何人都可以帮我解决这个小问题。 I think I am messing up with HTML 我想我搞砸了HTML

Current Timetable 当前时刻表

You probably need an empty cell in your first row that was showing start times, and then for every row with the subjects, add a cell that shows the day of the week. 您可能需要在第一行中显示开始时间的空白单元格,然后为包含主题的每一行添加一个显示星期几的单元格。

Also, I feel you might reduce your triple for-loop to at least a double for-loop if you use an ORDER BY clause in your SQL. 另外,我觉得如果在SQL中使用ORDER BY子句,则可能会将三重for循环减少到至少双倍for循环。 Maybe order by day, and then by start time (for the start time, you'll need a join with the lectures table). 也许按天排序,然后按开始时间排序(对于开始时间,您需要与讲课表连接)。 It also seems that the outer loop should iterate through days, and the inner loop - through start times. 似乎外循环应该迭代数天,而内循环则要迭代开始时间。

EDIT: You can even just have one loop, if you were to cross join distinct days and start times, order that by days and then start times, and then do a left outer join with your actual timetable. 编辑:如果您要交叉连接不同的日期和开始时间,甚至可以只有一个循环,按天数然后按开始时间排序,然后使用实际时间表进行左外部连接。 You will end up having NULLs in every row that correspond to a day and time that does not have a scheduled lecture. 您最终将在每一行中都有NULL,这些NULL与没有安排的讲座的日期和时间相对应。 You will also know that your query gives the same number of rows for every day. 您还将知道查询每天提供相同数量的行。 With that, you can just create a new <tr /> every N rows, where N is the number of start times in a day. 这样,您可以每N行创建一个新的<tr /> ,其中N是一天中的开始时间。

EDIT2: Regarding the loop nesting, I meant something like this: ` 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>";   
            }
    }

First, make sure you're calling the right MySQL table fields. 首先,请确保您要调用正确的MySQL表字段。

If you are, then, change your $days array into a bidirectional array with week days names, add day cell on the loop and an empty cell at the top. 如果是,则将$ days数组更改为带有工作日名称的双向数组,在循环中添加day单元格,在顶部添加一个空单元格。 (see code comments) (请参见代码注释)

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

?>

If you don't want to create a bidirectional array, use PHP's function jddayofweek() . 如果您不想创建双向数组,请使用PHP的函数jddayofweek() Here is an example: 这是一个例子:

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

}

?>

However, for performance reasons, I do recommend that you use the bidirectional array. 但是,出于性能原因,我建议您使用双向阵列。

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