我在数组中安装的PDO回声结果

时间:2015-08-23 18:12:40

标签: php sql pdo

我的代码有点问题。这段代码来自Joe Meyer,他帮助了我。

我的表格

stundenplan
+----+---------+-------+--------+---------+---------+------+
| id | user_id | tag   | stunde | fach    | lehrer  | raum |
+----+---------+-------+--------+---------+---------+------+
|  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    | ...
+---------+----------+----

PHP代码

//Vars
$user_id = htmlentities($_SESSION["user_id"], ENT_QUOTES, 'UTF-8');
$tage = array(1,2,3,4,5);
$stunden = array(1,2,3,4,5,6,7,8,9,10,11);

//SQL
$stmt = $pdo->prepare("
SELECT tag, stunde, fach, lehrer, raum 
FROM stundenplan 
    INNER JOIN users ON stundenplan.user_id = users.user_id 
WHERE users.user_id = :user_id
ORDER BY stundenplan.stunde, stundenplan.tag");
$stmt->bindValue(':user_id', $user_id);
$stmt->execute();

$record = array();
$record['stunde'] = 0;
$record['tag'] = 0;

echo'
<table class="table_standard">
    <tr>
        <th class="th_titlebar" style="white-space:nowrap">Std.</th>
        <th class="th_titlebar"><center>Mo</center></th>
        <th class="th_titlebar"><center>Di</center></th>
        <th class="th_titlebar"><center>Mi</center></th>
        <th class="th_titlebar"><center>Do</center></th>
        <th class="th_titlebar"><center>Fr</center></th>
    </tr>
';
foreach ($stunden as $stunde) {
    echo '<tr>';
    echo '<td align="middle" class="td_contentbar" style="white-space:nowrap">'.$stunde.'</td>';
    foreach ($tage as $tag) {                
        if($stunde > $record['stunde'] || ($tag > $record['tag'] && $stunde <= $record['stunde'])) {
            while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                $record = $row;
            }
        } 

        echo '<td align="middle" class="td_contentbar">';
        if($stunde == $record['stunde'] && $tag == $record['tag']) {
            echo '<input class="inputfeld sp_input" id="fach_' . $tag . '_' . $stunde . '" value="' . $record['fach'] . '">';
            echo '<input class="inputfeld sp_input" id="lehrer_' . $tag . '_' . $stunde . '" value="' . $record['lehrer'] . '">';
            echo '<input class="inputfeld sp_input" id="raum_' . $tag . '_' . $stunde . '" value="' . $record['raum'] . '">';
        } else {
            echo '<input class="inputfeld sp_input" placeholder="Fach" id="fach_' . $tag . '_' . $stunde . '">';
            echo '<input class="inputfeld sp_input" placeholder="Lehrer" id="lehrer_' . $tag . '_' . $stunde . '">';
            echo '<input class="inputfeld sp_input" placeholder="Raum" id="raum_' . $tag . '_' . $stunde . '">';
        }

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

输出

我想从每个html输入中获取数据库中的所有数据。但是使用这段代码我遇到的问题是它只需要sql的最后一个结果并将其放入适当的输入域。

在下面的图片中,如果数据库中有数据但是我的代码只输出了最后一个sql结果,则应填写所有字段。

enter image description here

1 个答案:

答案 0 :(得分:0)

我想那块

    echo '<td align="middle" class="td_contentbar">';
    if($stunde == $record['stunde'] && $tag == $record['tag']) {
        echo '<input class="inputfeld sp_input" id="fach_' . $tag . '_' . $stunde . '" value="' . $record['fach'] . '">';
        echo '<input class="inputfeld sp_input" id="lehrer_' . $tag . '_' . $stunde . '" value="' . $record['lehrer'] . '">';
        echo '<input class="inputfeld sp_input" id="raum_' . $tag . '_' . $stunde . '" value="' . $record['raum'] . '">';
    } else {
        echo '<input class="inputfeld sp_input" placeholder="Fach" id="fach_' . $tag . '_' . $stunde . '">';
        echo '<input class="inputfeld sp_input" placeholder="Lehrer" id="lehrer_' . $tag . '_' . $stunde . '">';
        echo '<input class="inputfeld sp_input" placeholder="Raum" id="raum_' . $tag . '_' . $stunde . '">';
    }

    echo '</td>';

需要进入while循环。