简体   繁体   English

如何在JavaScript和PHP中遍历对象

[英]How to Iterate through Object in JavaScript and PHP

I am having to solve a problem involving code in both JS and PHP. 我必须解决一个涉及JS和PHP中的代码的问题。 For some reason, whenever this code executes, it puts the first entry in all the rows of the table instead of iterating through each entry and putting all of them in the rows. 由于某种原因,无论何时执行此代码,它都会将第一个条目放置在表的所有行中,而不是遍历每个条目并将它们全部放入行中。 I would appreciate someone's help in giving me insights into how to fix this issue. 希望有人能为我提供有关如何解决此问题的真知灼见。 Can this be fixed with just a "for in" loop? 可以仅通过“ for in”循环来解决此问题吗? Thanks in advance. 提前致谢。

 <?php include('../../functions.php'); $query = " SELECT * FROM plobby LEFT JOIN users ON users.UID = plobby.UID WHERE `LID` = '". preg_replace("/[^A-Za-z0-9 ]/", '', $_POST['id']) ."'; "; $sql = "SELECT COUNT(`LID`) AS `x` FROM `snipe`.`plobby` WHERE LID = '".$_POST['id']."';"; $result = $db->query($query); $rst = $db->query($sql); $cnt = 0; if($rst->num_rows > 0) while($row = $rst->fetch_assoc()) $cnt = $row["x"]; if ($result->num_rows > 0) for($i = 1;$i<= $cnt;$i++) echo json_encode($result->fetch_assoc()); else echo json_encode([]); ?> 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

Here is the object to which the above loop is referring: 这是上面的循环所引用的对象:

 <script type="text/javascript"> var state = {}; for($i = 1;$i <= <?php echo getLobbytPlayers($_GET['id']);?>;$i++ ){ var reloadTable = function (data) { if ($.data(state) == $.data(data)) { return; } $('#js-lobby-table').empty(); $.each(data, function (rowNumber, rowData) { var row = $('<tr>'); console.log(data); // Player row.append($('<td>', { 'html': data.eName })); // Status row.append($('<td>', { 'html': data.gameID == "000" ? 'waiting' : 'ingame' })); // Win % row.append($('<td>', { 'html': 'TODO' })); // Games row.append($('<td>', { 'html': 'TODO' })); // K/D row.append($('<td>', { 'html': 'TODO' })); $('#js-lobby-table').append(row); }); // Set the current table state. state = data; }; } setInterval(function () { $.ajax({ type: 'POST', url: '/lobby/api/table.php', data: { id: '<?= $_GET['id'] ?>' }, success: reloadTable, dataType: 'json' }); }, 10); </script> 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

You shouldn't call json_encode() multiple times. 您不应多次调用json_encode() The response has to contain a single JSON object, not multiple objects. 响应必须包含一个JSON对象,而不是多个对象。 You need to put all the results in an array, and call json_encode() on that array at the end. 您需要将所有结果放入一个数组中,最后在该数组上调用json_encode()

There's also no need to get the count first. 也没有必要先获得计数。 Just call fetch_assoc() until you get all the results. 只需调用fetch_assoc()直到获得所有结果即可。

<?php include('../../functions.php');

    $query = "
    SELECT
    *
    FROM
    plobby
    LEFT JOIN users ON users.UID = plobby.UID
    WHERE
    `LID` = '". preg_replace("/[^A-Za-z0-9 ]/", '', $_POST['id']) ."';
    ";

    $result = $db->query($query);

    $rows = [];

    while ($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }
    echo json_encode($rows);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM