简体   繁体   English

使用HTML创建时间表-PHP-MySQL

[英]Create a timetable with HTML - PHP - MySQL

I want to make a timetable in a HTML-table. 我想在HTML表中创建一个时间表。 I have a database structure set up and now I have trouble with displaying the database data in the HTML-table correctly. 我已经建立了数据库结构,现在我无法在HTML表中正确显示数据库数据。

My database structure involves: 我的数据库结构涉及:

在此处输入图片说明

The days are numbered by day_nr where 1 = monday, 2 = tuesday, 3 = wednesday, 4 = thursday and 5 = friday. 这些天由day_nr编号,其中1 =星期一,2 =星期二,3 =星期三,4 =星期四,5 =星期五。 The hours are numbered by hour_nr from 1 to 10. 小时由hour_nr从1到10编号。

Here is an example of what I eventually want to create. 这是我最终想要创建的示例。

单击我的图像]

Some code: 一些代码:

<?php
session_start();
    // Connect MySQL Database
    require("function.php");
    $con = sql_connection();
        // Select data from database (select the right table)
    $result01 = mysqli_query("SELECT teacher_code, day_nr, hour_nr, subject_code, room_nr, class_code FROM `lesson`");

    echo "<table class='table table-striped table-bordered student_table'>
            <thead class='thead-inverse'> 
                <tr>
                    <th class='start_time'></th>
                    <th>Monday</th>
                    <th>Tuesday</th>
                    <th>Wednesday</th>
                    <th>Thursday</th>
                    <th>Friday</th>
                </tr>
            </thead>";

You need to create a data structure in your php program representing your time table. 您需要在php程序中创建一个表示时间表的数据结构。

Perhaps you should create an array with an element for each day. 也许您应该每天创建一个带有元素的数组。 Each element of that top level array can itself contain an array, with an element for each time slot in the day. 该顶级数组的每个元素本身都可以包含一个数组,其中包含一天中每个时隙的一个元素。

Then each time slot element can contain an array, with an element for each item to represent in the time slot. 然后,每个时隙元素可以包含一个数组,每个元素都有一个元素在时隙中表示。

You can then populate this data structure as you retrieve your MySQL result set. 然后,您可以在检索MySQL结果集时填充此数据结构。 You can then render it -- write it to HTML -- using PHP code. 然后,您可以使用PHP代码进行渲染-将其编写为HTML。

Explaining how to create and use of data structure is, in my opinion, beyond the scope of what you should expect in a Stack Overflow answer. 我认为,解释如何创建和使用数据结构超出了Stack Overflow答案的预期范围。

You have just echoed your table. 您刚刚在桌子上回声了。 now you need to echo the actual element in your database for that: you might consider this 现在您需要为此回显数据库中的实际元素:您可以考虑一下

if($num = mysqli_num_rows($result01) > 1){
while($res=mysqli_fetch_assoc[$result01]){
    echo "<tr><td>";
    echo $res['name of your attribute in the database'];
    echo "</td><td></tr>";
}

} }

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

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