php中的表重复

时间:2018-01-29 09:21:46

标签: php html

我正在处理小型项目,我必须从数据库中显示数据,但是这样做时,我的网站会复制它。 例如,您在数据库中存储了2个不同的数据(第一个数据是1,第二个数据是2),每当我输出1和2,而不是在一个表中显示1和2时,整个表重复(包括标题,这是我的问题开始的地方)但我也不希望我的标题也复制。

            <?php 

            include_once 'dbh.inc.php';
                $sqli = "SELECT datum, v, vo, nav FROM  months;";
                $result = mysqli_query($conn, $sqli);
                $resultCheck = mysqli_num_rows($result);

                    if ($resultCheck > 0) {
                        while ($row = mysqli_fetch_assoc($result)) {
                            if($row['datum'] AND $row['v'] AND $row['vo'] AND $row['nav']) {

            echo "<table>";
                        echo "<tr>";
                            echo "<th>Datum</th>";
                            echo "<th> Vakantie uren</th>";
                            echo "<th> Vakantie uren opgennomen </th>";
                            echo "<th> Nieuw aantal vakantie uren</th>";
                        echo "</tr>";
                echo "<tr>";
                echo "<td>".$row['datum']."</td>";
                echo "<td>".$row['v']."</td>";
                echo "<td>".$row['vo']."</td>";
                echo "<td>".$row['nav']."</td>";
                echo "</tr>";
            echo "</table>";
            }
        }
}   
        ?>

我已经尝试了不同的方法,例如将TH放在表中,为它们创建一个新表,将它们放在if和while语句之上。什么都没有帮助。

亲切的问候和非常感谢! 饰面

4 个答案:

答案 0 :(得分:0)

您的代码中的错误是您还在while循环中打印表头。例如,如果表中有3行,则会将它们全部输出到三个表中。

我更改为代码以解决此问题,首先打印表html和表头,然后在循环中迭代以打印数据,并在while循环外部关闭表。如果有效,请尝试使用此代码。

  <?php 

                include_once 'dbh.inc.php';
                    $sqli = "SELECT datum, v, vo, nav FROM  months;";
                    $result = mysqli_query($conn, $sqli);
                    $resultCheck = mysqli_num_rows($result);
                      if ($resultCheck > 0) {
                            echo "<table>";
                            echo "<tr>";
                                echo "<th>Datum</th>";
                                echo "<th> Vakantie uren</th>";
                                echo "<th> Vakantie uren opgennomen </th>";
                                echo "<th> Nieuw aantal vakantie uren</th>";
                            echo "</tr>";
                            while ($row = mysqli_fetch_assoc($result)) {
                                if($row['datum'] AND $row['v'] AND $row['vo'] AND $row['nav']) {


                    echo "<tr>";
                    echo "<td>".$row['datum']."</td>";
                    echo "<td>".$row['v']."</td>";
                    echo "<td>".$row['vo']."</td>";
                    echo "<td>".$row['nav']."</td>";
                    echo "</tr>";
                }
            }
                    echo "</table>";
    }   
            ?>

答案 1 :(得分:0)

当你想要打印一个表时,table标签应该在循环语句之外,重复的部分应该在循环中。

<?php 

include_once 'dbh.inc.php';
$sqli = "SELECT datum, v, vo, nav FROM  months;";
$result = mysqli_query($conn, $sqli);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
    echo "<table>";
            echo "<tr>";
                echo "<th>Datum</th>";
                echo "<th> Vakantie uren</th>";
                echo "<th> Vakantie uren opgennomen </th>";
                echo "<th> Nieuw aantal vakantie uren</th>";
            echo "</tr>";

    while ($row = mysqli_fetch_assoc($result)) {
        if($row['datum'] AND $row['v'] AND $row['vo'] AND $row['nav']) {
              echo "<tr>";
              echo "<td>".$row['datum']."</td>";
              echo "<td>".$row['v']."</td>";
              echo "<td>".$row['vo']."</td>";
              echo "<td>".$row['nav']."</td>";
              echo "</tr>";
              }
        }
    echo "</table>";
}   
?> 

答案 2 :(得分:0)

由于标题位于循环中,您会得到export class PersonEntity { public name: string; public email: string; } 个重复项。 您应该使用标题th移动表格标记<table></table>和表格行<tr></tr> 在循环之外。 首先打开<th></th>代码,显示表格标题<table>

<th>

然后显示每一行结果:

echo "<table>";
    echo "<tr>";
        echo "<th>Datum</th>";
        echo "<th> Vakantie uren</th>";
        echo "<th> Vakantie uren opgennomen </th>";
        echo "<th> Nieuw aantal vakantie uren</th>";
    echo "</tr>";

最后,最后关闭表标签

if ($resultCheck > 0) {
    while ($row = mysqli_fetch_row($result)) {
        echo "<tr>";
        echo "<td>".$row['datum']."</td>";
        echo "<td>".$row['v']."</td>";
        echo "<td>".$row['vo']."</td>";
        echo "<td>".$row['nav']."</td>";
        echo "</tr>";
        }
    }

答案 3 :(得分:-1)

不要回显<?php include_once 'dbh.inc.php'; $sqli = "SELECT datum, v, vo, nav FROM months;"; $result = mysqli_query($conn, $sqli); $resultCheck = mysqli_num_rows($result); if ($resultCheck > 0) { echo "<table>"; echo "<thead>"; echo "<tr>"; echo "<th>Datum</th>"; echo "<th> Vakantie uren</th>"; echo "<th> Vakantie uren opgennomen </th>"; echo "<th> Nieuw aantal vakantie uren</th>"; echo "</tr>"; echo "</thead>"; echo "<tbody>"; while ($row = mysqli_fetch_assoc($result)) { if($row['datum'] AND $row['v'] AND $row['vo'] AND $row['nav']) { echo "<tr>"; echo "<td>".$row['datum']."</td>"; echo "<td>".$row['v']."</td>"; echo "<td>".$row['vo']."</td>"; echo "<td>".$row['nav']."</td>"; echo "</tr>"; } } echo "</tbody>"; echo "</table>"; } ?> 循环中的表格标签,将它们移到外面。

{{1}}