【问题标题】:PHP/HTML - Table Not Displaying CorrectlyPHP/HTML - 表格显示不正确
【发布时间】:2018-05-16 00:08:44
【问题描述】:

目前我有一个链接到 MySQL 数据库的表,它从数据库中获取行。

效果很好,数组显示也很好。

但不是一个普通的表格每次都显示它,而是像下面这样并排显示它。

出于隐私原因,玩家姓名被空白。但空白处是播放​​器所在的位置。

这是数据库连接文件:

<?php
// MySQL Connection Details
$mysql_host = ""; // Host name 
$mysql_username = ""; // Mysql username 
$mysql_password = ""; // Mysql password 
$mysql_database = ""; // Database name 
$mysql_table = ""; // Table name 

// MySQL Connection
$con = mysqli_connect("$mysql_host","$mysql_username","$mysql_password","$mysql_database");

// MySQL Error Logging
if (mysqli_connect_errno())
  {
  echo "MySQL Connection Failed: " . mysqli_connect_error();
  }

$sql = "SELECT * FROM $mysql_table";
$result = $con->query($sql);  

?>

下面是它在主 php 文件中的显示方式。

<table id="ldr_table">
    <tr class="lb_tb_hd" align="center">
        <td>Player</td>
        <td>Points</td>
        <td>Wins</td>
        <td>Kills</td>
        <td>Deaths</td>
        <td>Played</td>
    </tr>
    <tr class="ldr_alt">
        <?php
        require('{db connection file}');
        while($row = mysqli_fetch_array($result, MYSQLI_BOTH))
        {   
        echo '<td><center>' . $row['player_name'] . '</center></td>';
        echo '<td><center>' . $row['score'] . '</center></td>';
        echo '<td><center>' . $row['games_won'] . '</center></td>';
        echo '<td><center>' . $row['kills'] . '</center></td>';
        echo '<td><center>' . $row['deaths'] . '</center></td>';
        echo '<td><center>' . $row['games_played'] . '</center></td>';
        }
        mysqli_close($con);
        ?>
    </tr>
</table>

我已经尝试了几件事,包括foreach,这对格式没有影响。

如果需要其他文件,我会很乐意在这里展示它们。

【问题讨论】:

  • 是否应该将您的 tr 改为您的 while,以便您获得一个包含所有数据的有效行作为一个 retruend 行,然后关闭表格行,然后再次执行 while 循环?
  • 您不需要在变量周围加上引号,例如$mysql_host,除非您打算使用它添加文本。

标签: php html mysql sql html-table


【解决方案1】:

您需要为每个 SQL 结果创建一个新行

<?php
require('{db connection file}');
while($row = mysqli_fetch_array($result, MYSQLI_BOTH))
{
     echo '<tr class="ldr_alt">';
     echo '<td><center>' . $row['player_name'] . '</center></td>';
     echo '<td><center>' . $row['score'] . '</center></td>';
     echo '<td><center>' . $row['games_won'] . '</center></td>';
     echo '<td><center>' . $row['kills'] . '</center></td>';
     echo '<td><center>' . $row['deaths'] . '</center></td>';
     echo '<td><center>' . $row['games_played'] . '</center></td>';
     echo '</tr>';
}
mysqli_close($con);

?>

PS:使用

text-align: center;

而不是多个中心应答器。

【讨论】:

    【解决方案2】:

    在while循环中回显&lt;tr&gt;标签。

    while($row = mysqli_fetch_array($result, MYSQLI_BOTH))
    {
        echo '<tr class="ldr_alt">';
        echo '<td><center>' . $row['player_name'] . '</center></td>';
        echo '<td><center>' . $row['score'] . '</center></td>';
        echo '<td><center>' . $row['games_won'] . '</center></td>';
        echo '<td><center>' . $row['kills'] . '</center></td>';
        echo '<td><center>' . $row['deaths'] . '</center></td>';
        echo '<td><center>' . $row['games_played'] . '</center></td>';
        echo '</tr>';
    }
    

    【讨论】:

      【解决方案3】:

      .在while循环中放置&lt;tr&gt;标签

      <table id="ldr_table">
              <tr class="lb_tb_hd" align="center">
                  <td>Player</td>
                  <td>Points</td>
                  <td>Wins</td>
                  <td>Kills</td>
                  <td>Deaths</td>
                  <td>Played</td>
              </tr>
      
                  <?php
                  require('{db connection file}');
                  while($row = mysqli_fetch_array($result, MYSQLI_BOTH))
                  {   
                  echo '<tr class="ldr_alt">'
                  echo '<td><center>' . $row['player_name'] . '</center></td>';
                  echo '<td><center>' . $row['score'] . '</center></td>';
                  echo '<td><center>' . $row['games_won'] . '</center></td>';
                  echo '<td><center>' . $row['kills'] . '</center></td>';
                  echo '<td><center>' . $row['deaths'] . '</center></td>';
                  echo '<td><center>' . $row['games_played'] . '</center></td>';
                  echo '</tr>'
                  }
                  mysqli_close($con);
                  ?>
      
          </table>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-09
        • 1970-01-01
        • 1970-01-01
        • 2022-01-13
        • 1970-01-01
        相关资源
        最近更新 更多