我在一些 MySQL 表中存储了值。我需要选择它们并通过 PHP 传入 HTML 表。 HTML 表格应如下图所示:
期望输出
代码
我正在使用以下代码:
<table>
<tr>
<th>First Name </th> //There are headers shown in image
<th>Middle Name</th>
<th>Last Name </th>
</tr>
<tr>
<th>Rank </th>
<th>Rank Applied </th>
<th>Date Applied </th>
<th>Date Availability </th>
<th>Vessels Type </th>
</tr>
<tr>
<th>DOB </th>
<th>POB </th>
<th>Nationality </th>
<th>English </th>
</tr>
........
<?php
while ($users->fetch()) {
?>
<tr>
<td><?php echo $FirstName; ?></td> // there are variables selected from MySQL tables
<td><?php echo $MiddleName; ?></td>
<td><?php echo $LastName; ?></td>
</tr>
<tr>
<td><?php echo $Rank; ?></td>
<td><?php echo $RankApplied; ?></td>
<td><?php echo $DateAvailability; ?></td>
<td><?php echo $VesselsType; ?></td>
</tr>
<tr>
<td><?php echo $DOB; ?></td>
<td><?php echo $POB; ?></td>
<td><?php echo $Nationality; ?></td>
<td><?php echo $English; ?></td>
</tr>
........
<?php
}
?>
</table>
目前输出错误
我不知道如何实现如上图所示的表结构。现在我的结构如下:
// All headers going here.....
First Name Middle Name Last Name
Rank Rank Applied Date Availability VesselsType
DOB POB Nationality English
............
// All values below headers....
John - Anderson
foo@bar.com 5545565445
Something 1 Something 2 Something 3
...........
正如您在示例中看到的,首先打印所有标题,然后打印所有值,但每个标题应该高于该值。
我不知道HTML表的结构是否错误,或者PHP循环是否有问题,或者我应该使用CSS做一些事情>...?你有什么想法吗?如果有不清楚的地方 - 问我,我会提供更多详细信息。
最佳答案
将整个表单结构移至循环中,因为您希望每个循环都有一个新表:
<?php
while ($users->fetch()) {
?>
<table>
<tr>
<th>First Name </th> //There are headers shown in image
<th>Middle Name</th>
<th>Last Name </th>
</tr>
<tr>
<td><?php echo $FirstName; ?></td> // there are variables selected from MySQL tables
<td><?php echo $MiddleName; ?></td>
<td><?php echo $LastName; ?></td>
</tr>
<tr>
<th>Rank </th>
<th>Rank Applied </th>
<th>Date Applied </th>
<th>Date Availability </th>
<th>Vessels Type </th>
</tr>
<tr>
<td><?php echo $Rank; ?></td>
<td><?php echo $RankApplied; ?></td>
<td><?php echo $DateAvailability; ?></td>
<td><?php echo $VesselsType; ?></td>
</tr>
<tr>
<th>DOB </th>
<th>POB </th>
<th>Nationality </th>
<th>English </th>
</tr>
<tr>
<td><?php echo $DOB; ?></td>
<td><?php echo $POB; ?></td>
<td><?php echo $Nationality; ?></td>
<td><?php echo $English; ?></td>
</tr>
</table>
<?php
}
?>
关于php - 通过 PHP + HTML 从 MySQL 数据库中选择数据创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36240085/