首页 > 解决方案 > 数据显示在调用它的前端上方

问题描述

我有一个简单的类来查看在我的 Web 应用程序中被列入黑名单的人。这是 index.php

require_once "../model/ViewBlacklist.php";
// Handling a navigation to the webpage
try {
    $viewBlacklist = new ViewBlacklist("view-blacklists.php", "View Blacklists", $navForUser);
} catch (Exception $ex) {
    echo $ex->getMessage();
}

这是ViewBlacklist课程:

class ViewBlacklist {

    function __construct(string $page, string $pageTitle, string $navbar) {

        if (empty($_POST) && empty($_GET) && empty(file_get_contents("php://input"))) {
            $_SESSION["CSRFTOKEN"] = hash("sha512", random_bytes(64));
            $csrfToken = $_SESSION["CSRFTOKEN"];
            $pageTitle = $pageTitle;
            $navForUser = $navbar;
            $usernameBlacklist = "<table id='usernameBlacklistTable'>";
            $usernameBlacklist .= "<thead><tr><th>Username</th><th>Time Blocked</th><th>When time will expire</th><th>Remove Blacklist?</th></tr></thead>";
            $usernameBlacklist .= "<tbody>";
            $usernameBlacklist .= $this->getBlacklistedUsernames();
            $usernameBlacklist .= "</tbody>";
            $usernameBlacklist .= "</table>";
            $ipAddressBlacklist = "<table id='ipAddressBlacklistTable'>";
            $ipAddressBlacklist .= "<thead><th>Ip Address</th><th>Time Blocked</th><th>When time will expire</th><th>Remove Blacklist?</th></thead>";
            $ipAddressBlacklist .= "<tbody>";
            $ipAddressBlacklist .= $this->getBlacklistedIps();
            $ipAddressBlacklist .= "</tbody>";
            $ipAddressBlacklist .= "</table>";
            include_once $page;
            exit;
        }

    }

    private function getBlacklistedUsernames() : string {
        $blacklistedUsernames = "";
        $initDB = new DatabaseConfig();
        $db = $initDB->start_db();
        $sql = "SELECT username, added, allowed FROM blacklisted_users";
        $stmt = $db->prepare($sql);
        $stmt->execute();
        $data = $stmt->fetch(PDO::FETCH_ASSOC);
        $stmt->closeCursor();
        if (!empty($data)) {
            var_dump($data);
        } else {
            return "<tr><p class='empty-table-return'>There are no blacklisted usernames</p></tr>";
        }
    }

    private function getBlacklistedIps() : string {
        $blacklistedUsernames = "";
        $initDB = new DatabaseConfig();
        $db = $initDB->start_db();
        $sql = "SELECT ip_add, added, allowed FROM blacklisted_ips";
        $stmt = $db->prepare($sql);
        $stmt->execute();
        $data = $stmt->fetch(PDO::FETCH_ASSOC);
        $stmt->closeCursor();
        if (!empty($data)) {
            var_dump($data);
        } else {
            return "<tr><p class='empty-table-return'>There are no blacklisted IP addresses</p></tr>";
        }
    }

}

问题是,我在哪里调用$this->getBlacklistedUsernames();$this->getBlacklistedIps()从该数据返回(当前在 else 中,因为现在我的黑名单表中没有任何内容)正在我的表上方,所以这些词There are no blacklisted usernames出现在包含在整个<table>元素之上变量$usernameBlacklist和IP地址相同。以下是它在前端的调用方式:

<?php include_once "../header.php"; include_once "../nav.php"; ?>

<?php echo $usernameBlacklist; echo $ipAddressBlacklist; ?>

<?php include_once "../footer.php";

是什么让我的get...()函数返回出现在它们连接的变量上方?

标签: php

解决方案


所以问题是,它实际上是一个 HTML 验证问题。事实证明,你不能让一个元素成为不是元素的元素的直接<tr>后代<td>。所以这是一个问题的原因是因为我的<p>元素没有<td>围绕它的父元素,所以 html 验证失败迫使它<p>到达顶部。


推荐阅读