2015-12-14 52 views
1

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

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

回答

0

先手伸头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> 
+0

感谢它的工作,但现在我哈哈另一个问题,我把表格放在一个div中,所以我可以在我的CSS样式中,但现在它只将一行数据放在div中,其余部分放在div中。 – MarcusFenix1

+0

如何更新您的脚本 –