水平标题会继续显示每一行。我希望标题只显示在顶行。我的代码出了什么问题?提前谢谢。
$sql = $conn->prepare("SELECT * FROM timeSheet, timeSheetUsers WHERE timeSheet.userName=timeSheetUsers.userName AND timeSheet.userName=? ORDER BY startTime ASC");
if ($sql->execute(array($_SESSION['userName']))) {
while ($row = $sql->fetch()) {
echo "<table border ='1'>";
echo
"<tr>
<th>username</th>
<th>date</th>
<th>starTime</th>
<th>endTime</th>
<th>total</th>
</tr>
<tr>
<td>" . $row['userName'] . "</td>
<td>" . $row['date'] . "</td>
<td>" . $row['startTime'] . "</td>
<td>" . $row['endTime'] . "</td>
<td>" . $row['total'] . "</td>
</tr>";
echo "</table>";
}
}
答案 0 :(得分:3)
你已经在循环中为table(th)提供了包含标题的行,更改它并使它们在while循环中运行
像
$sql = $conn->prepare("SELECT * FROM timeSheet, timeSheetUsers WHERE timeSheet.userName=timeSheetUsers.userName AND timeSheet.userName=? ORDER BY startTime ASC");
if ($sql->execute(array($_SESSION['userName']))) {
echo "<table border ='1'>";
echo "<tr>
<th>username</th>
<th>date</th>
<th>starTime</th>
<th>endTime</th>
<th>total</th>
</tr>";
while ($row = $sql->fetch()) {
echo
" <tr>
<td>" . $row['userName'] . "</td>
<td>" . $row['date'] . "</td>
<td>" . $row['startTime'] . "</td>
<td>" . $row['endTime'] . "</td>
<td>" . $row['total'] . "</td>
</tr>";
}
echo "</table>";
}
答案 1 :(得分:1)
标题不应该是while循环的一部分,这应该可行
if ($sql->execute(array($_SESSION['userName']))) {
echo "<table border ='1'>";
echo
"<tr>
<th>username</th>
<th>date</th>
<th>starTime</th>
<th>endTime</th>
<th>total</th>
</tr>";
while ($row = $sql->fetch()) {
echo "<tr>
<td>" . $row['userName'] . "</td>
<td>" . $row['date'] . "</td>
<td>" . $row['startTime'] . "</td>
<td>" . $row['endTime'] . "</td>
<td>" . $row['total'] . "</td>
</tr>";
}
echo "</table>";
}