繁体   English   中英

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

[英]Table headings displaying for each new row in 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 

只是在循环时出头:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM