2015-08-23 57 views
-2

我有我的代碼有點問題。這段代碼來自幫助我的Joe Meyer。PDO回聲結果,我在一個數組中安全

我的表

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

+0

這不幫助我。我應該如何編輯我的代碼? – Bernd

回答

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循環。

+0

當我將這添加到while循環時,html表看起來很奇怪,但我得到了第一個sql結果。 – Bernd