【问题标题】:Table headings displaying for each new row in table为表格中的每个新行显示表格标题
【发布时间】:2016-03-20 12:45:29
【问题描述】:

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

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

【问题讨论】:

    标签: php html mysql html-table


    【解决方案1】:

    在while循环中走开:

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

    【讨论】:

    • 谢谢它的工作,但现在我有另一个问题,我把表格放在一个 div 中,所以我可以在我的 css 中设置样式,但现在它只将其中一行数据放在 div 中,其余的放在外面。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多