通过从MySQL数据库中选择数据,通过PHP + HTML创建表

时间:2016-03-26 19:47:20

标签: php html mysql sql html-table

我在一些 MySQL 表中存储了值。我需要选择它们并通过 PHP 传入 HTML 表。 HTML 表应如下图所示:

渴望输出

sample table

CODE

我使用以下代码:

<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 循环可能出错,或者我应该用< EM> CSS 的......?你有什么想法吗?如果事情不清楚 - 请问我,我会提供更多细节。

1 个答案:

答案 0 :(得分:0)

将整个表单结构移动到循环中,因为每个循环都需要一个新表:

<?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 
} 
?>