简体   繁体   English

在JS和PHP中添加到数组后,仅显示表的某些列

[英]Only Some Columns of Table Display after Added to Array in JS and PHP

I am trying to get all columns and rows to appear after being added to an array in JS and PHP. 我正在尝试将所有列和行添加到JS和PHP中的数组后显示。 For some reason, neither the player column nor the status column are filled with content. 由于某些原因,播放器列和状态列均未填充内容。 Any insight into this is appreciated. 对此有任何见识,不胜感激。

This is the code where the array is collected: 这是收集数组的代码:

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

And this is the code where the table originates: 这是表起源的代码:

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

When you debug, look at the values in your variables to ensure they are what you expect them to be. 调试时,请查看变量中的值以确保它们符合您的期望。 Your code is treating these variables inconsistently. 您的代码不一致地对待这些变量。 For example, you're looping over data : 例如,您正在遍历data

$.each(data ...

Which implies that data is an array. 这意味着data是一个数组。 But then you also try to access values on data : 但是,然后您也尝试访问data值:

'html': data.eName

I suspect that you actually want to access values on rowData within that iteration: 怀疑您实际上是想在该迭代中访问rowData值:

'html': rowData.eName

(Same error a few more lines down in your code.) (同一错误在您的代码中多了几行。)

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

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