php - 如何在带有表的特定div中显示mysql数据

标签 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;
}

最佳答案

发生这种情况是因为您已将 </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);
?>

关于php - 如何在带有表的特定div中显示mysql数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21367846/

相关文章:

php - 拉拉维尔。加入后保持 Eloquent 关系

PHP Zend 与 AWS ElastiCache

MySQL嵌套查询查找错误

php - 查询返回重复结果

css - CSS中容器的扩展宽度

javascript - Jquery 或 Javascript : How to find all html elements which has image more than 500px and less than 2000px?

html - 如何在用户离开输入后设置输入样式

php - 在 php 中将 ip 转换为整数的正确方法是什么?

javascript - jquery ajax 成功操作参数无法正常工作

javascript - 如何在 hh :mm:ss format in javascript 中一次增加或减少百分比