我想撰寫一個函式,首先從我的資料庫中檢索所有帳戶資料,然后將它們分類到一個用戶友好的 HTML 表中。在那里,用戶可以選擇一些帳戶并更改它們。這是我的代碼:
$.ajax({
url: "./database.php",
type: "POST",
data: {
todo: "adminGetAccountTable",
b: userInfo["UserId"],
c: userInfo["Password"],
session: userInfo["SessionName"]
},
success: function(response) {
$("#loadingIcon").css("display", "none");
console.log(response);
if (response !== "No accounts registered") {
var json = JSON.parse(response);
var accountsTable = "<table class='w3-table-all' id='accountsTable'><thead><tr><th> </th><th><b class='link link-primary' id='00'>NUTZER-ID </a></th><th><b class='link link-primary' id='01'>ERSTELLDATUM </a></th><th><b class='link link-primary' id='02'>NAME </a></th><th><b class='link link-primary' id='03'>VORNAME </a></th><th><b class='link link-primary' id='04'>E-MAIL-ADRESSE </a></th><th><b class='link link-primary' id='05'>GESAMTSTUNDEN </a></th><th><b class='link link-primary' id='06'>ACCOUNTTYP </a></th></tr></thead><tbody>";
var index = 0;
for (var current in json) { // 'json' is the data coming from my database
accountsTable = "<tr>";
accountsTable = "<td><input type='checkbox' onchange='toggleSelectedEntry(" index ", true);'></td>";
// More user information gets written here
index ;
}
accountsTable = "</tbody></table>";
document.getElementById("accountsDiv").innerHTML = accountsTable; // Add the table to the div container
}
}
});
這些條目都有一個唯一的 id(當前索引)。然后,該函式toggleSelectedEntry()從存盤所有選定條目的陣列中添加/洗掉條目。
唯一的問題是瀏覽器決議這個引數是錯誤的。它不是決議0, 1, 2, 3, 4等等,而是以奇怪的順序決議它們:0, 4, 1, 2, 3. 奇怪的是,在執行時console.log(accountsTable),索引以正確的順序顯示。只有在瀏覽器撰寫和決議后,才會出現這種奇怪的順序。
Does someone know what could cause this? I can also send more code if that helps, I just didn't want to clutter my question too much.
Edit: Here's the code from database.php that I use to obtain the data:
$stmt = $this->connection->prepare("SELECT UserId, Created, FirstName, LastName, Email, IF(PermissionId = 2, 'Admin', 'Nutzer') AS PermissionString FROM users");
$stmt->execute();
$stmtResult = $stmt->get_result();
$rows = array();
while ($r = mysqli_fetch_assoc($stmtResult)) {
$rows[] = $r;
}
if ($rows == array()) {
error_log("Cancelled Interaction: No accounts registered");
echo("No accounts registered");
exit;
} else {
foreach ($rows as &$value) {
foreach ($value as &$field) {
$field = utf8_encode($field);
}
}
$outputValue = json_encode($rows, JSON_UNESCAPED_UNICODE);
echo(utf8_decode($outputValue));
exit;
This returns something like:
[{"UserId":"61fe559d1dd35\u0000\u0000\u0000","Created":"2022-02-05 11:46:53","FirstName":"Nico","LastName":"Marikucza","Email":"[email protected]","PermissionString":"Admin"},{"UserId":"62041298682a0\u0000\u0000\u0000","Created":"2022-02-09 20:14:32","FirstName":"Peter","LastName":"Marikucza","Email":"[email protected]","PermissionString":"Nutzer"}]
uj5u.com熱心網友回復:
我自己弄清楚了(在評論的幫助下)。
我發現我在 JS 中使用了一個函式來LastName按升序對資料進行排序。但這與資料庫的輸出不匹配,導致程式錯誤地分配了數字。我也通過簡單地通過 MySQL 對輸出進行排序來解決此問題:
SELECT UserId, Created, FirstName, LastName, Email, IF(PermissionId = 2, 'Admin', 'Nutzer') AS PermissionString FROM users ORDER BY LastName ASC
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/452206.html
標籤:javascript html dom
