我的pdo结果在表格中重复

时间:2015-12-16 23:12:34

标签: php html pdo html-table

我有一个排行榜,告诉我人们的钱是什么(用于测试目的的虚拟货币)。 所以它可以工作,它通过 PDO(PHP数据对象)显示从我的表到的所有内容。

问题是,我让它在表中输出。但是对于每个id,theres都是这样的第二个表:

enter image description here

我正在使用的代码:

<?php 
$db = new PDO('mysql:host=localhost;dbname=test1','root','####'); 

$sql = "SELECT naam, id, days, money FROM user ORDER BY money DESC "; 
$results = $db->query($sql); 



foreach($results as $row) 
{ 

?>


<html>
<head>



</head>
<body>
<TABLE BORDER="5"    WIDTH="100%"   CELLPADDING="4" CELLSPACING="3">
   <TR>
      <TH><BR><H3>POINTS LEADERBOARD</H3>
      </TH>
   </TR>
   <TR>
      <TH>Username</TH>
      <TH>Money</TH>
      <TH>Days</TH>
   </TR>
   <TR>
      <TD><?php echo $row['naam']; ?></TD>
      <TD><?php echo $row['money']?></TD>
      <TD><?php echo $row['days']?></TD>
   </TR>
</TABLE>

<?php
/*
echo "<table>";
echo "<TR>";
    echo "<TH><BR><H3>POINTS LEADERBOARD</H3>";
    echo "</TH>";
echo "</TR>";

echo "<TR>";
echo "<TH>Username</TH>";
echo "<TH>Money</TH>";
echo "<TH>Days</TH>";
echo "</TR>";

echo "<TR>";
    echo "<TD>";    echo $row['naam']; echo "</TD>";
    echo "<TD>";    echo $row['money']; echo "</TD>";
    echo "<TD>";    echo $row['days']; echo "</TD>";
echo "</TR>";
echo "</table>";
*/
?>




<?php } ?>
</body>
</html>

任何人都可以帮助我或告诉我我做错了什么吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

你在循环中打开html等标签的方式是错误的 - 也许是这样的?

<?php 
    $db = new PDO('mysql:host=localhost;dbname=test1','root','####'); 
    $sql = 'SELECT naam, id, days, money FROM user ORDER BY money DESC '; 
    $results = $db->query($sql); 
?>
<html>
    <head>
        <title>leaderboard</title>
    </head>
    <body>
        <?php
        echo "
            <TABLE BORDER='5' WIDTH='100%' CELLPADDING='4' CELLSPACING='3'>
               <TR>
                  <TH><BR><H3>POINTS LEADERBOARD</H3></TH>
               </TR>
               <TR>
                  <TH>Username</TH>
                  <TH>Money</TH>
                  <TH>Days</TH>
               </TR>";

        foreach($results as $row) { 
            echo "
               <TR>
                  <TD>{$row['naam']}</TD>
                  <TD>{$row['money']}</TD>
                  <TD>{$row['days']}</TD>
               </TR>";
        }
        echo "</TABLE>";
        ?>
    </body>
</html>