我试图填充我的html表使用数据从我的数据库,但每一个新的行被添加到表,表向下移动。例如,如果向表中添加了38行,则该表变得不可见。你必须向下滚动才能看到表格。问题是什么呢?
echo "
<div class='container'>
<span class='cc' style='font-weight: bold;'>Count: $stat</span></br></br>
<table class='col-md-9' border='1'>
<th>Agent's name</th>
<th>Student's name</th>
<th>Student's number</th>
<th>Accommodation type</th>
<th>School</th>
<th>Date & Time</th>
";
foreach($db->query($select) AS $result){
echo "
<tr><td>{$result['agent_name']}</td>
<td>{$result['student_name']}</td>
<td>{$result['student_number']}</td>
<td>{$result['accommodation']}</td>
<td>{$result['school']}</td>
<td>{$result['contact_date']}</td>
</tr>
</br>
";
}
echo "
</table>
</div>
";
不能将<th>
直接放在<table>
中。请用<tr>
包好。
为了获得结果行,必须从foreach
获取行。你要循环的就是stdObject ResultSet
。将代码改为:
$result = $db->query($select)
if (mysqli_num_rows($result) > 0)
while (false != ($row = mysqli_fetch_array($result)))
// Loop here.
是的,正如其他人所说,<table>
标签只能包含<tbody>
, <thead>
, <tfoot>
和<tr>
。什么都没有。没有像</br>
这样的标签。必须是<br />
。复习HTML的基础知识。
两件事:
首先,需要将header元素换行:
<tr> <!-- here -->
<th>Agent's name</th>
<th>Student's name</th>
<th>Student's number</th>
<th>Accommodation type</th>
<th>School</th>
<th>Date & Time</th>
</tr> <!-- and here -->
第二,这些到底是什么?:
</br>
浏览器可能会这样解释它们:
<br />
这意味着你在每一行的表结构中添加了一个换行符。这可能是无效的(因为它在表单元格之外,并且是表本身的一部分),但也可能是每一行将显示进一步向下推的原因。把它从循环中删除
目前代码正在生成无效的表标记,因此表的样式和呈现将在某种程度上未定义,并且可能因每个浏览器而异。
你需要把你的
<th></th>
也包裹在<tr></tr>
中,
确实如此。
其次,我建议在这种情况下不要使用echo
。下面是代码的样子,我将这样做:
<div class='container'>
<span class='cc' style='font-weight: bold;'><?php Count: $stat ?></span></br></br>
<table class='col-md-9' border='1'>
<tr>
<th>Agent's name</th>
<th>Student's name</th>
<th>Student's number</th>
<th>Accommodation type</th>
<th>School</th>
<th>Date & Time</th>
</tr>
<?php
foreach($db->query($select) AS $result):
?>
<tr>
<td><?=$result['agent_name']?></td>
<td><?=$result['student_name']?></td>
<td><?=$result['student_number']?></td>
<td><?=$result['accommodation']?></td>
<td><?=$result['school']?></td>
<td><?=$result['contact_date']?></td>
</tr>
</br>
<?php
endif;
?>
</table>
</div>
这将是我更喜欢的代码,因为您将在代码编辑器中保留HTML的颜色,这使得其他人更容易调试。
小解释:<?=
和?>
很容易使用打开和关闭标记来回显HTML中的变量