简体   繁体   中英

How to Iterate through Object in JavaScript and PHP

I am having to solve a problem involving code in both JS and 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? 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. The response has to contain a single JSON object, not multiple objects. You need to put all the results in an array, and call json_encode() on that array at the end.

There's also no need to get the count first. Just call fetch_assoc() until you get all the results.

<?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);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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