dongya2030 2016-05-03 00:13
浏览 45
已采纳

如何设置一个部分html和部分php的表格

I found it hard to phrase this question but I will try my best. I am creating a php and MYSQL leaderboard, it all works but now I want to style it. I don't know very much css and this might be a simple fix. I am trying to style the table so that all the data table elements sit symmetrical like a table should be, and aligned to the center of the headers. I think it is not working because I technically have 2 tables I am trying to style and they are not working in conjunction with each other. Here is the code and forgive my usernames in the table, I get lazy sometimes.

<!DOCTYPE html>
<html>
<head>
<title>Leaderboards</title>
<style type="text/css">
th {
  overflow: auto;
  font-size: 25px;
  border: 1px solid;
  padding: 5px;
  margin: 2px 2px 2px 2px;
}
td {
  font-size: 25px;
  padding-left: 30px;
  padding-top: 3px;
}
</style>
</head>
<body>
<h1>Leaderboard</h1>
<table>
<tr>
    <th>Rank</th>
    <th>User</th>
    <th>Score</th>
</tr>
</table>
<?php
include 'HytecFunctions.php';
$conn=connectDB();

$rank = 1;

  $sql = 'SELECT Name, Score FROM Names ORDER BY Score DESC';
foreach ($conn->query($sql) as $row) {
echo "<table>
        <td>$rank</td>
        <td>$row[Name]</td>
        <td>$row[Score]</td>
        </tr>
        </table>";
$rank++;
}

  $conn->close();
?>
</body>
</html>

The table as it shows in my browser

  • 写回答

1条回答 默认 最新

  • dongwuxie5112 2016-05-03 00:16
    关注

    Because you try to create another table every loop. Try to put them all in a single table.

    <table>
        <tr>
            <th>Rank</th>
            <th>User</th>
            <th>Score</th>
        </tr>
    
        <?php
    
            include 'HytecFunctions.php';
            $conn=connectDB();
    
            $rank = 1;
    
            $sql = 'SELECT Name, Score FROM Names ORDER BY Score DESC';
            foreach ($conn->query($sql) as $row) {
                echo '<tr>
                          <td>'.$rank.'</td>
                          <td>'.$row["Name"].'</td>
                          <td>'.$row["Score"].'</td>
                      </tr>';
                $rank++;
            }
    
            $conn->close();
    
        ?>
    </table>
    

    Note: Don't mind much the way I input your $row variables. Yours will still work even if you use double tick (") to display rows. I just use a single tick (') to display your data as is to have a much cleaner look on your code. Refer here for the difference.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP