php - 数据显示在调用它的前端上方
问题描述
我有一个简单的类来查看在我的 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...()
函数返回出现在它们连接的变量上方?
解决方案
所以问题是,它实际上是一个 HTML 验证问题。事实证明,你不能让一个元素成为不是元素的元素的直接<tr>
后代<td>
。所以这是一个问题的原因是因为我的<p>
元素没有<td>
围绕它的父元素,所以 html 验证失败迫使它<p>
到达顶部。
推荐阅读
- flutter - ShowDialog Context: 找到了这个候选人,但参数不匹配
- iis - 是否需要在 IIS 10 上安装 CORS 模块?
- javascript - 使用 window:scroll 时捕获 div id
- capacitor - Android中的电容器相机预览上方未显示按钮
- javascript - v-for 循环在运行后打印空行
- php - 从验证 MessageBag 中替换消息数组
- c# - How to update Enum value in ASP.NET Core MVC?
- ios - __OBJC2__ 在哪里?
- wordpress - 为类别的 woocommerce 单品添加自定义文本
- java - can you help me how to breakdown the denomination of my change? I've been searching everywhere how to do it but I still don't get it