表不是集中的

时间:2014-12-26 17:41:27

标签: php mysql html-table

<?php
  $query = "select * from patient ";
  $result = mysqli_query($connection , $query);

  while ($count = mysqli_fetch_assoc($result)) {

      # code...
  echo  "<table class='table table-hover'><thead><tr>".
                "<th>id</th>
                <th>Name</th>
                <th>phone</th>
                <th>Date</th>
                <th>Address</th>
                <th>Delete</th>
                <th>update</th>
              </tr>
            </thead>
            <tbody>
              <tr>";?>
              <?php if (isset($count['name'])) { 
               echo  "<td>" . $count['id']. "</td>
                <td>" .ucwords($count['name']) ."</td>
                <td>". $count['phone'] . "</td>
                <td>" .$count['addeddate'] ."</td>
                <td>". ucwords($count['address']). "</td>
                <td>". "</td>
                <td>".  "</td>
              </tr>

            </tbody>
          </table>";
      }
  }
 ?>

我从数据库中获取数据并将其显示在表格中,但是一旦这段代码一次又一次地重复标题,我想显示表格标题。有什么帮助吗?

3 个答案:

答案 0 :(得分:2)

移动

<table class='table table-hover'><thead><tr>".
                "<th>id</th>
                <th>Name</th>
                <th>phone</th>
                <th>Date</th>
                <th>Address</th>
                <th>Delete</th>
                <th>update</th>
              </tr>
            </thead>
            <tbody>

从while循环到循环外

<?php
  $query = "select * from patient ";
  $result = mysqli_query($connection , $query);
<table class='table table-hover'><thead><tr>".
                "<th>id</th>
                <th>Name</th>
                <th>phone</th>
                <th>Date</th>
                <th>Address</th>
                <th>Delete</th>
                <th>update</th>
              </tr>
            </thead>
            <tbody>
  while ($count = mysqli_fetch_assoc($result)) {

      # code...
  echo  "
              <tr>";?>
              <?php if (isset($count['name'])) { 
               echo  "<td>" . $count['id']. "</td>
                <td>" .ucwords($count['name']) ."</td>
                <td>". $count['phone'] . "</td>
                <td>" .$count['addeddate'] ."</td>
                <td>". ucwords($count['address']). "</td>
                <td><a href='delete.php?id=".$count['id']."'>delete</a></td>
                <td></td>
              </tr>

            ";
      }
  }
    ?>
</tbody>
          </table>

其中delete.php有删除条目的代码,相应的id作为查询字符串发送到delete.php

答案 1 :(得分:1)

您需要将表格页眉和页脚移出while循环。

答案 2 :(得分:0)

<?php

$query = "select * from patient ";
$result = mysqli_query($connection, $query);

$html = "<table class='table table-hover'>
    <thead>
    <tr>
         <th>id</th>
         <th>Name</th>
         <th>phone</th>
         <th>Date</th>
         <th>Address</th>
         <th>Delete</th>
         <th>update</th>
     </tr>
    </thead>
     <tbody>";


while ($count = mysqli_fetch_assoc($result)) {
    if (isset($count['name'])) {
        $html .= "<tr><td>" . $count['id'] . "</td>
                <td>" . ucwords($count['name']) . "</td>
                <td>" . $count['phone'] . "</td>
                <td>" . $count['addeddate'] . "</td>
                <td>" . ucwords($count['address']) . "</td>
                <td>" . "</td>
                <td>" . "</td>
              </tr>";
    }
}

echo $html .= "</tbody></table>";
?>

评论回答:删除和更新链接的最后两个帖子

        <td><a href='delete.php?id=" . $count['id'] . "'>Delete</a></td>
        <td><a href='update.php?id=" . $count['id'] . "'>update</a></td>