PHP SQL表标题重复

时间:2014-12-29 01:48:18

标签: php sql

代码可以工作,但每个新数据集都会重复标题。我希望标题设置在顶部而不会重复...不知道是什么导致它。

----------------------------------------------- -------------------------------------------------- --------------------- randomtextrandomtextrandomtextrandomtextrandomtextrandomtextrandomtextrandomtextrandomtext ---------------------------- -------------------------------------------------- -------------------------------------------------- ---------------------------

$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT deadchar, level, class, killforumid, realm, date FROM pkdata ORDER BY deadchar DESC";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {


$deadchar = $row['deadchar'];
$level = $row['level'];
$class = $row['class'];
$killforumid = $row['killforumid'];
$realm = $row['realm'];
$date = $row['date'];

$conn->close();

    echo
        "<table><center><tr>

            <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

           <font size=3 color=#070719>Victim</font>

           &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
            <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

            <font size=3 color=#070719>Level</font>

            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
            <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

           <font size=3 color=#070719>Class</font>

           &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
            <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

           <font size=3 color=#070719>Killer</font>

           &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
            <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

           <font size=3 color=#070719>Realm</font>

            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
            <th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

            <font size=3 color=#070719>Date</font>

            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>

            </tr></center>";




        echo
            "<tr><center>

            <td><center>$deadchar</center></td>
            <td><center>$level</center></td>
            <td><center>$class</center></td>
            <td><center>$killforumid</center></td>            
            <td><center>$realm</center></td>
            <td><center>$date</center></td>           

            </tr>";

    echo "</table>";




}

?>

2 个答案:

答案 0 :(得分:1)

你的代码非常混乱。

你永远不应该使用

&nbsp;

布局代码。

现在你的问题,你的标题正在重复,因为你在while()循环中有它们,因此对于每一个数据,它将再次创建标题。

    echo
        "<table><tr>

            <th style='text-align:center'><font size=3 color=#070719>Victim</font></th>
            <th style='text-align:center'><font size=3 color=#070719>Level</font></th>
            <th style='text-align:center'><font size=3 color=#070719>Class</font></th>
            <th style='text-align:center'><font size=3 color=#070719>Killer</font></th>
            <th style='text-align:center'> <font size=3 color=#070719>Realm</font></th>
            <th style='text-align:center'><font size=3 color=#070719>Date</font></th>

            </tr>";





$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT deadchar, level, class, killforumid, realm, date FROM pkdata ORDER BY deadchar DESC";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {


$deadchar = $row['deadchar'];
$level = $row['level'];
$class = $row['class'];
$killforumid = $row['killforumid'];
$realm = $row['realm'];
$date = $row['date'];
        echo
            "<tr>

            <td style='text-align:center'>".$deadchar."</td>
            <td style='text-align:center'>".$level."</td>
            <td style='text-align:center'>".$class."</td>
            <td style='text-align:center'>".$killforumid."</td>            
            <td style='text-align:center'>".$realm."</td>
            <td style='text-align:center'>".$date."</td>           

            </tr>";

}

echo "</table>";


$conn->close();


?>

试试这个,它会工作

答案 1 :(得分:0)

从循环中删除表标题。把它们放在循环之上,这不会发生。请确保将</table>置于循环之外/之后。

编辑:

<?php
  $conn = new mysqli($servername, $username, $password, $dbname);
  $sql = "SELECT deadchar, level, class, killforumid, realm, date FROM pkdata ORDER BY deadchar DESC";
  $result = $conn->query($sql);

  // initialize your variable (not needed, but okay to do at the top to keep things organized if you have multiple variables at play.)
  $myTable = '';

  // the ".=" appends to a variable once it is initialized already
  $myTable .= 
    '<table>
        <tr>
          <th>Victim</th>
          <th>Level></th>
          <th>Class</th>
          <th>Killer</th>
          <th>Realm</th>
          <th>Date</th>
        </tr>';

  while($row = $result->fetch_assoc())
  {
    $deadchar = $row['deadchar'];
    $level = $row['level'];
    $class = $row['class'];
    $killforumid = $row['killforumid'];
    $realm = $row['realm'];
    $date = $row['date'];

    $myTable .= '
        <tr>
          <td>'.$deadchar.'</td>
          <td>'.$level.'</td>
          <td>'.$class.'</td>
          <td>'.$killforumid.'</td>            
          <td>'.$realm.'</td>
          <td>'.$date.'</td>           
        </tr>';
  }
  $myTable .= '</table>';
  $conn->close();
?>
<html>
<head>
<style>
  /* Here is where you enter your styles instead of in your PHP */
  table {
    text-align:center;
    margin:0 auto;
  }
  th, td {
    font-size:3; 
    color:#070719
  }
</style>
</head>
<body>
  <!-- This will echo out your table -->
  <?php echo $myTable; ?>
</body>
</html>