将MySQL结果保存到php多维数组(时间表)

时间:2015-08-21 17:31:31

标签: php mysql arrays

我的表格:

timetable
+----+---------+-------+--------+---------+---------+------+
| id | user_id | s_day | s_hour | subject | teacher | room |
+----+---------+-------+--------+---------+---------+------+
|  1 |       1 |     1 |      1 | MATH    | SM      | 101  |
|  2 |       1 |     1 |      2 | MATH    | SM      | 101  |
|  3 |       1 |     1 |      3 | MATH    | SM      | 101  |
|  4 |       1 |     1 |      4 | MATH    | SM      | 101  |
|  5 |       1 |     1 |      5 | MATH    | SM      | 101  |
|  6 |       1 |     1 |      6 | MATH    | SM      | 101  |
|  7 |       1 |     2 |      1 | MATH    | SM      | 101  |
|  8 |       1 |     2 |      2 | MATH    | SM      | 101  |
| .. |     ... |   ... |    ... |     ... |     ... |  ... |

users
+---------+----------+----
| user_id | username | ...
+---------+----------+----
|       1 | User1    | ...
+---------+----------+----

现在我想将此时间表输出到带有输入字段的html表中。

HTML表格

enter image description here

<tr> //this would be the row for the first hour
    <td align="middle" class="td_contentbar" style="white-space:nowrap">1</td>
    <td align="middle" class="td_contentbar"><input class="inputfeld sp_input" id="subject_1_1"><input class="inputfeld sp_input" id="teacher_1_1"><input class="inputfeld sp_input" id="room_1_1"></td>
    <td align="middle" class="td_contentbar"><input class="inputfeld sp_input" id="subject_2_1"><input class="inputfeld sp_input" id="teacher_2_1"><input class="inputfeld sp_input" id="room_2_1"></td>
    <td align="middle" class="td_contentbar"><input class="inputfeld sp_input" id="subject_3_1"><input class="inputfeld sp_input" id="teacher_3_1"><input class="inputfeld sp_input" id="room_3_1"></td>
    <td align="middle" class="td_contentbar"><input class="inputfeld sp_input" id="subject_4_1"><input class="inputfeld sp_input" id="teacher_4_1"><input class="inputfeld sp_input" id="room_4_1"></td>
    <td align="middle" class="td_contentbar"><input class="inputfeld sp_input" id="subject_5_1"><input class="inputfeld sp_input" id="teacher_5_1"><input class="inputfeld sp_input" id="room_5_1"></td>
</tr>
<tr> //this would be the row for the 2nd hour
    <td align="middle" class="td_contentbar" style="white-space:nowrap">2</td>
    <td align="middle" class="td_contentbar"><input class="inputfeld sp_input" id="subject_1_2"><input class="inputfeld sp_input" id="teacher_1_2"><input class="inputfeld sp_input" id="room_1_2"></td>
    <td align="middle" class="td_contentbar"><input class="inputfeld sp_input" id="subject_2_2"><input class="inputfeld sp_input" id="teacher_2_2"><input class="inputfeld sp_input" id="room_2_2"></td>
    <td align="middle" class="td_contentbar"><input class="inputfeld sp_input" id="subject_3_2"><input class="inputfeld sp_input" id="teacher_3_2"><input class="inputfeld sp_input" id="room_3_2"></td>
    <td align="middle" class="td_contentbar"><input class="inputfeld sp_input" id="subject_4_2"><input class="inputfeld sp_input" id="teacher_4_2"><input class="inputfeld sp_input" id="room_4_2"></td>
    <td align="middle" class="td_contentbar"><input class="inputfeld sp_input" id="subject_5_2"><input class="inputfeld sp_input" id="teacher_5_2"><input class="inputfeld sp_input" id="room_5_2"></td>
</tr>
输入ID的

语法:

subject_5_2
subject/teacher/room _ day (5 -> friday) _ hour (2 -> 2nd)

我考虑将完整的mysql结果放在一个多维数组中,如下所示:

$timetable[day][hour]

但我怎么能这样做或者这种方法愚蠢吗?或者我应该完全重新设计系统吗?

1 个答案:

答案 0 :(得分:2)

我实际上没有对此进行测试,但理论上它应该让你非常接近。它还可以处理不存在记录的情况,但是您需要在顶行中的数组中定义您的日期/小时,以便输出预期。

由于您的每一行都按小时输出,然后逐日输出,我建议您按顺序输出:

//create a list of days / hours to check
$days = array(1,2,3,4,5);
$hours = array(1,2,3,4,5,6,7,8,9,10,11);


$stmt = $pdo->prepare("SELECT * 
    FROM timetable 
        INNER JOIN users ON timetable.user_id = users.user_id 
    WHERE users.username=:username
    ORDER BY timetable.s_hour, timetable.s_day");

// bind the parameters so we only show User1's results
$stmt->bindValue(":username", 'User1');
//run the query
$stmt->execute();

$record = array();
$record['s_hour'] = 0;
$record['s_day'] = 0;
foreach ($hours as $hour) {
    echo '<tr>';
    echo '<td align="middle" class="td_contentbar" style="white-space:nowrap">' . $hour . '</td>';
    foreach ($days as $day) {                
        //if the current slot we're adding data to comes after the record we're currently holding, grab the next record
        if(isset($record['s_hour']) && isset($record['s_day']) 
           && $hour > $record['s_hour'] ||
           ($day > $record['s_day'] && $hour <= $record['s_hour')) {
            if($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $record = $row;
            }
        } 

        //if this record, matches the hour/day slot we're on, output the appropriate data, else output an empty row
        echo '<td align="middle" class="td_contentbar">'
        if($hour == $record['s_hour'] && $day == $record['s_day']) {
            echo '<input class="inputfeld sp_input" id="subject_' . $day . '_' . $hour . '" value="' . $record['subject'] . '">';
            echo '<input class="inputfeld sp_input" id="teacher_' . $day . '_' . $hour . '" value="' . $record['teacher'] . '">';
            echo '<input class="inputfeld sp_input" id="room_' . $day . '_' . $hour . '" value="' . $record['room'] . '">'
        } else {
            echo '<input class="inputfeld sp_input" id="subject_' . $day . '_' . $hour . '">';
            echo '<input class="inputfeld sp_input" id="teacher_' . $day . '_' . $hour . '">';
            echo '<input class="inputfeld sp_input" id="room_' . $day . '_' . $hour . '">'
        }

        echo '</td>'
    }
    echo '</tr>';
}