2015-06-25 111 views
-1

我有一個使用pdo的mysql查詢,問題是數據不會在表中排隊。有什麼建議麼?我希望它看起來像一個標準的HTML表或其他東西。我願意接受任何建議。從PDO格式化數據Mysql查詢

foreach($results as $row) { 
echo "<table width='900'>"; 
echo "<tr>"; 
    echo "<td>"; 
    echo htmlspecialchars($row['serial']) . "</td>"; 
    echo "<td>"; 
    echo htmlspecialchars($row['model']) . "</td>"; 
    echo "<td>"; 
    echo htmlspecialchars($row['deviceCondition']) . "</td>"; 
    echo "<td>"; 
    echo htmlspecialchars($row['sealCondition']) . "</td>"; 
    echo "<td>"; 
    echo htmlspecialchars($row['location']) . "</td>"; 
    echo "<td>"; 
    echo htmlspecialchars($row['deployDate']) . "</td>"; 
    echo "<td>"; 
    echo htmlspecialchars($row['weight']) . "</td>"; 
    echo "<td>"; 
    echo htmlspecialchars($row['notes']) . "</td>"; 
echo "</tr>"; 
echo "</table>"; 

    //Arrays 
    $serials [] = htmlspecialchars($row['serial']); //serial as an array 
    $models [] = htmlspecialchars($row['model']); //model as an array 
    $deviceConditions [] = htmlspecialchars($row['deviceCondition']);//deviceCondition as an array 
    $locations [] = htmlspecialchars($row['location']); //location as an array 
    $deployDates [] = htmlspecialchars($row['deployDate']); //deployDate as an array 
    $weights [] = htmlspecialchars($row['weight']); //weight as an array 
    $notess [] = htmlspecialchars($row['notes']); //notes as an array 
} 
+1

地方你'

'標籤和''外循環。你正在爲每一行創建 –

+1

有時你想知道爲什麼你會打擾 – RiggsFolly

+0

分配給數組時不要使用'htmlspecialchars'。這應該只在網頁上顯示時使用。 – Barmar

回答

1

如果您正確地生成HTML,事情會更好。

所以覺得你的代碼之前

echo "<table width='900'>"; 

foreach($results as $row) { 
    echo "<tr>"; 

    echo "<td>"; 
    echo htmlspecialchars($row['serial']) . "</td>"; 
    echo "<td>"; 
    echo htmlspecialchars($row['model']) . "</td>"; 
    echo "<td>"; 
    echo htmlspecialchars($row['deviceCondition']) . "</td>"; 
    echo "<td>"; 
    echo htmlspecialchars($row['sealCondition']) . "</td>"; 
    echo "<td>"; 
    echo htmlspecialchars($row['location']) . "</td>"; 
    echo "<td>"; 
    echo htmlspecialchars($row['deployDate']) . "</td>"; 
    echo "<td>"; 
    echo htmlspecialchars($row['weight']) . "</td>"; 
    echo "<td>"; 
    echo htmlspecialchars($row['notes']) . "</td>"; 
    echo '</tr>'; 

    //Arrays 
    $serials [] = htmlspecialchars($row['serial']); //serial as an array 
    $models [] = htmlspecialchars($row['model']); //model as an array 
    $deviceConditions [] = htmlspecialchars($row['deviceCondition']);//deviceCondition as an array 
    $locations [] = htmlspecialchars($row['location']); //location as an array 
    $deployDates [] = htmlspecialchars($row['deployDate']); //deployDate as an array 
    $weights [] = htmlspecialchars($row['weight']); //weight as an array 
    $notess [] = htmlspecialchars($row['notes']); //notes as an array 
} 

echo "</table>"; 
+0

''應該仍然在循環中。 – Barmar

+0

是啊是錯別字固定 – RiggsFolly

+0

是的,這是我的錯。我原來的評論沒有包含tr標籤。我正在想別的東西 –