如何使用表在特定div中显示mysql数据

时间:2014-01-26 19:04:23

标签: php mysql css

我正在尝试将数据输出显示在表格中的特定div中。 我能够获得表设置并在表中显示数据的第一行,但输出的其余部分不会使其在表内。 任何人都可以指出我做错了什么? 谢谢!

<?php include('header.php'); ?>

<?php
     include_once 'scripts/db.php';
     $result = mysqli_query($con,"SELECT * FROM employee");
?>
    <div id="userlist">

      <center><h3>User Listing</h3></center>

       <?php
        echo "<table border='1'>
        <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>location</th>
        </tr>";
       ?>

       <?php
        while($row = mysqli_fetch_array($result))
       {?>

            <div class="userlistoutput">
                <?php
                echo "<td width=120px>" . $row['firstname'] . "</td>";
                echo "<td width=120px>" . $row['lastname'] . "</td>";
                echo "<td width=120px>" . $row['location'] . "</td>";
                echo "</tr>";
                ?>
            </div>

       <? echo "</table>" ?>
       <?php
       }?>
    </div>

<?php 
mysqli_close ($con);
?>

这是用户列表的CSS:

#userlist {
list-style:none;
width: auto;
margin:30px auto 1px auto;
height:100%;
padding:0px 20px 0px 20px;

/* Rounded Corners */

border-radius: 10px;

/* Background color and gradients */

background:#F4F4F4;
background: -moz-linear-gradient(top, #EEEEEE, #BBBBBB); */
background: -webkit-gradient(linear, 0% 0%, 0% 100%, from(#EEEEEE), to(#BBBBBB));

/* Borders */

border: 1px solid #002232;
}

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您已将</table>放在while语句中。尝试将其更改为:

<?php include('header.php');
 include_once 'scripts/db.php';
 $result = mysqli_query($con,"SELECT * FROM employee");
?>
<div id="userlist">

  <center><h3>User Listing</h3></center>

   <?php
    echo "<table border='1'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>location</th>
    </tr>";

    while($row = mysqli_fetch_array($result))
    {
            echo "<tr class='userlistoutput'>;
            echo "<td width='120px'>" . $row['firstname'] . "</td>";
            echo "<td width='120px'>" . $row['lastname'] . "</td>";
            echo "<td width='120px'>" . $row['location'] . "</td>";
            echo "</tr>";
   }
   echo "</table>";
   echo "</div>";
   mysqli_close ($con);
?>
相关问题