我的数据库数据显示不佳

时间:2018-07-11 17:00:07

标签: php html mysql bootstrap-4

我想从用户数据库中选择所有用户。代码可以工作,但是我想我错过了一些有关语法以及如何回显它的信息……因此它实际上检索到了他们,但是为每个数据生成了不同的表。 ...

我附上了图片

<?php
            // seclect users from table
            $selectUsers = "SELECT * FROM users ORDER BY userId DESC";
            $selectUserKwery = mysqli_query($link, $selectUsers);
            $userCount = mysqli_num_rows($selectUserKwery);
            if($userCount > 0){
            while ($userRow = mysqli_fetch_array($selectUserKwery)) { ?>
            <div class="table-responsive">
              <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                <thead>
                  <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Join Date</th>
                    <th>ACTION</th>
                  </tr>
                </thead>
                <tfoot>
                  <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Join Date</th>
                    <th>ACTION</th>
                  </tr>
                </tfoot>
                <tbody>
                  <tr>
                    <td><?php echo ucwords($userRow['userId']); ?></td>
                    <td><?php echo ucwords($userRow['userName']); ?></td>
                    <td>Edinburgh</td>
                    <td>61</td>
                    <td><p><a><i class="fa fa-fw fa-eye"></i></a> | <a><i class="fa fa-fw fa-edit"></i></a> | <a><i class="fa fa-fw fa-user-times"></i> </a> </p></td>
                  </tr>
                </tbody>
              </table>
                <?php     

            }
            } else if($userCount > 0) {
                        echo '<div class"alert alert-danger alert-dismissable">There are no Users yet</div>';
                } ?>

enter image description here

所以我希望数据库结果显示在一个表中

新结果

enter image description here

1 个答案:

答案 0 :(得分:0)

由于<table>while中,因此您将获得多个表。您应该将表的结构元素移到while之外,而只需在while中创建行。像

?>
            <div class="table-responsive">
              <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                <thead>
                  <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Join Date</th>
                    <th>ACTION</th>
                  </tr>
                </thead>
                <tfoot>
                  <tr>
                    <th>ID</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Join Date</th>
                    <th>ACTION</th>
                  </tr>
                </tfoot>
                <tbody>
<?php while ($userRow = mysqli_fetch_array($selectUserKwery)) { ?>
                  <tr>
                    <td><?php echo ucwords($userRow['userId']); ?></td>
                    <td><?php echo ucwords($userRow['userName']); ?></td>
                    <td>Edinburgh</td>
                    <td>61</td>
                    <td><p><a><i class="fa fa-fw fa-eye"></i></a> | <a><i class="fa fa-fw fa-edit"></i></a> | <a><i class="fa fa-fw fa-user-times"></i> </a> </p></td>
                  </tr>
<?php }/* close while */ ?>
                </tbody>
              </table>

应该这样做。您还可能应该在构建<table>之前检查是否有结果。