[英]Displaying data retrieved from database as table headers
我在顯示我的數據庫時遇到困難,該數據庫是我從數據庫中檢索到的列標題。 這是我的代碼,可以完美地工作,但是唯一的問題是它以行顯示我的數據,而我希望將其顯示為列標題。
<?php
require('db.php');
$stmt = $conn->prepare("SELECT * FROM new_emp");
$stmt -> execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
echo "<thead>";
echo "<td>".$row['emp_name']."</td>";
echo "<thead>";
}
?>
您需要對Table row
使用<tr>
。 將<th>
用作Table Header
或將<td>
用作Table data
。
<?php
require('db.php');
$stmt = $conn->prepare("SELECT * FROM new_emp");
$stmt -> execute();
$result = $stmt->fetchAll();
foreach ($result as $row) {
echo "<thead>";
echo "<tr><th>".$row['emp_name']."</th></tr>";
echo "<thead>";
}
?>
嘗試這個
<?php
require('db.php');
$stmt = $conn->prepare("SELECT * FROM new_emp");
$stmt -> execute();
$result = $stmt->fetchAll();
echo "<thead><tr>";
foreach ($result as $row) {
foreach($row as $c)
echo "<th>".$c."</th>";
}
echo "</tr></thead>";
?>
替換為可以解決您的目的,但是您編寫的代碼將添加多個表頭。 如果您的要求很好。 否則,您需要按以下方式更改代碼。
<?php
require('db.php');
$stmt = $conn->prepare("SELECT * FROM new_emp");
$stmt -> execute();
$result = $stmt->fetchAll();
echo "<thead><tr>";
foreach ($result as $row) {
echo "<th>".$row['emp_name']."</th></tr>";
}
echo "</th></thead>";
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.