显示表格中每个新行的表格标题

时间:2015-12-14 21:44:12

标签: php html mysql html-table

我正在创建一个表,显示数据库中表的数据。每次在表格中添加新行时,表格标题都会显示在每一行的上方。我如何得到它,所以桌子顶部只有标题? 这是我的代码

<?php
    $sql = "SELECT fName, sName, DOB
            FROM Customertbl";
    $result = mysqli_query($connection, $sql);

if (mysqli_num_rows($result) > 0)
{
     while($row = mysqli_fetch_assoc($result)) 
     {
    ?>                                          
<table style ="width:100%">
<tr>
    <th>Firstname</th>
    <th>Surname</th>
    <th>Date of Birth</th>
</tr>
<tr>
    <td><?php echo $row["fName"] ?></td>
    <td><?php echo $row["sName"] ?></td>
    <td><?php echo $row["DOB"] ?></td>
</tr>
</table>    
        <?php
    }
} 
?>

我页面上的输出就像这样

Firstname Surname Date of Birth
James     Scott   01/01/2000 
Firstname Surname Date of Birth
James     Scott   01/01/2000 

我想要这样

Firstname Surname Date of Birth
James     Scott   01/01/2000 
James     Scott   01/01/2000 

1 个答案:

答案 0 :(得分:0)

在循环中跳出来:

<table style ="width:100%">
    <tr>
        <th>Firstname</th>
        <th>Surname</th>
        <th>Date of Birth</th>
    </tr>
<?php
        $sql = "SELECT fName, sName, DOB
                FROM Customertbl";
        $result = mysqli_query($connection, $sql);

    if (mysqli_num_rows($result) > 0)
    {
         while($row = mysqli_fetch_assoc($result)) 
         {
        ?>                                          

    <tr>
        <td><?php echo $row["fName"] ?></td>
        <td><?php echo $row["sName"] ?></td>
        <td><?php echo $row["DOB"] ?></td>
    </tr>

            <?php
        }
    } 
    ?>
</table>