将Json数据发送到要在表单上回显的表上

时间:2018-12-05 05:40:35

标签: php json

我已经搜索了数周,以找到答案。我有json数据,我需要回显该页面并根据该名称旁边的按钮自动填写表格。这是我拥有的代码清单 杰森

$url = 'http://***.***.***.***:30120/players.json'; 
// path to your JSON file
$data = file_get_contents($url); 
// put the contents of the file into a variable
$players = json_decode($data); // decode the JSON feed

<?php foreach ($players as $players) : ?>
								  <tr>
								   <td>			
                                   <button>Ban</button>
                                   <td input type="hidden" name="user" value="user"><?php echo $players->name; ?></td>
								   <td input type="hidden" name="id0" value="id0"><?php echo $players->identifiers[0]; ?></td>
								   <td input type="hidden" name="id1" value="id1"><?php echo $players->identifiers[1]; ?></td>
								   </td>
								   </tr>
								   <?php endforeach; ?>

请给我

https://gyazo.com/e72aee4c977e9226f2de3a2053f89c2f

,我希望它自动用玩家姓名填写表格。

或类似的东西

function getpnames()
{
    $url = 'http://***.***.***.***:30120/players.json'; // path to your JSON file
    $data = file_get_contents($url); // put the contents of the file into a variable
    $players = json_decode($data); // decode the JSON feed
    $site = BASE_URL;
    
      

    $result = json_decode($data);

    echo '
        <table id="allUsers" class="table table-striped table-bordered">
        <thead>
            <tr>
            <th>Steam ID</th>
            <th>License</th>
            <th>Banned Player</th>
            <th>Admin</th>
            <th>Reason</th>
            <th>Date</th>
            <th>Perm?</th>
            <th>Actions</th>
            </tr>
        </thead>
        <tbody>
    ';

    while ($row = http_build_query($result, '', ';'))
    {
        echo '
        <tr>
            <td>' . $row[1] . '</td>
            <td>' . $row[2] . '</td>
            <td>' . $row[3] . '</td>
            <td>' . $row[4] . '</td>
            <td>' . $row[5] . '</td>
            <td>' . $row[6] . '</td>
            <td>' . $row[7] . '</td>
            <td>
                <form action="'.$site.'/actions/adminActions.php" method="post">
                <input name="deleteBan" type="submit" class="btn btn-xs btn-link" onclick="deleteBan(' . $row[0] . ')" value="Delete" />
                ';
       
        echo '

                <input name="bid" type="hidden" value=' . $row[0] . ' />
                </form>
            </td>
        </tr>
        ';
    }

    echo '
        </tbody>
        </table>
    ';

}

我似乎无法为此包住头

Json数据

  

[{“ endpoint”:“ 127.0.0.1”,“ id”:206,“ identifiers”:[“ steam:1100001366011b6”,“ license:”],“ name”:“ Bonsan905”,“ ping”: 21},{“ endpoint”:“ 127.0.0.1”,“ id”:183,“ identifiers”:[“ steam:11000010b7f88b5”,“ license:”,“ xbl:2535467507102211”,“ live:844425221417841”],“ name“:” Jonathan J。“,” ping“:34},{” endpoint“:” 127.0.0.1“,” id“:190,” identifiers“:[” steam:110000110be22ca“,” license:“,” xbl:2535451382352605“,”活动:1899947295907508“”,“名称”:“ [TT-AMRA III] Aqua”,“ ping”:47},{“ endpoint”:“ 127.0.0.1”,“ id”:135, “ identifiers”:[“ steam:1100001161c23dc”,“ license:”,“ xbl:2535429725972000”,“ live:914802232166266”],“ name”:“ Norank。”,“ ping”:19},{“ endpoint”: “ 127.0.0.1”,“ id”:160,“ identifiers”:[“ steam:110000106d4e97c”,“ license:”,“ xbl:2535436731105061”,“ live:844428383945858”],“ name”:“ Max W.” ,“ ping”:66},{“ endpoint”:“ 127.0.0.1”,“ id”:181,“ identifiers”:[“ steam:11000010a812251”,“ license:”,“ xbl:2535415227958286”,“ live: 1688853869063259“],”名称“:” Filippen“,” ping“:40},{”端点“:” 127.0.0.1“,” id“:202,”标识符“:[” steam:110000134bddbd7“,” li cense:“,” xbl:2535453098180945“,”活动:914802201806712“],”名称“:” VAC_brotha“,” ping“:44},{” endpoint“:” 127.0.0.1“,” id“:243,”标识符“:[” steam:1100001351ef803“,”许可证:“,” xbl:2533274991210897“,”实时:985156970449557“],”名称“:”艾伦[S-662]“,” ping“:177},{”端点“:” 127.0.0.1“,” id“:218,”标识符“:[” steam:11000010ad8b0bf“,”许可证:“,” xbl:2533274880694377“,”活动:1759221802890599“],”名称“:” Katie Kat“,” ping“:166},{” endpoint“:” 127.0.0.1“,” id“:184,” identifiers“:[” steam:11000010be231cc“,” license:“,” xbl:2533275012889196“,”直播:1759222203382981“]”“名称”:“ High Budget Trucker T-69”,“ ping”:55},{“ endpoint”:“ 127.0.0.1”,“ id”:244,“ identifiers”:[“ steam :110000115f03632“,”许可证:“],”名称“:” John Adams“,” ping“:72},{” endpoint“:” 127.0.0.1“,” id“:223,” identifiers“:[” steam :11000010bb69ddd“,”许可证:“,”实时:1899943264922053“],”名称“:” [S853] Johnathen [1Z13]“,” ping“:45},{” endpoint“:” 127.0.0.1“,” id “:216,” identifiers“:[” steam:11000011cb29587“,”许可证:“],”名称“:” Steve“,” ping“:198},{”端点“:” 127.0.0.1“,” id“ :203, “ identifiers”:[“ steam:1100001076829a3”,“ license:”,“ xbl:2535446122759170”,“ live:1055521739595821”],“ name”:“ Hoopsure”,“ ping”:152},{“ endpoint”:“ 127.0.0.1“,” id“:226,”标识符“:[” steam:1100001359fd857“,”许可证:“],”名称“:” [S-894] EpicDogePlayzYT“,” ping“:183},{”端点“:” 127.0.0.1“,” id“:236,”标识符“:[” steam:11000010d29c185“,”许可证:“],”名称“:”夜“,” ping“:24},{”端点“:” 127.0.0.1“,” id“:193,”标识符“:[” steam:1100001183e1fa8“,”许可证:“],”名称“:” JW“,” ping“:201},{”端点“ :“ 127.0.0.1”,“ id”:219,“标识符”:[“ steam:11000013559122f”,“许可证:”],“名称”:“ [S-885] Damien.B “,” ping“:61},{” endpoint“:” 127.0.0.1“,” id“:187,” identifiers“:[” steam:110000108413e7a“,” license:“,” xbl:2535423985280873“,” live :985156647647394“],”名称“:” Purple Deathh“,” ping“:18},{” endpoint“:” 127.0.0.1“,” id“:130,” identifiers“:[” steam:110000113e184ad“,”许可证:“,” xbl:2535441445938947“,”活动:844428429722616“],”名称“:” JamiDoesStuff“,” ping“:23},{”端点“:” 127.0.0.1“,” id“:205,”标识符“:[” steam:11000011a84d 2dc“,” license:“],” name“:” Johnny“,” ping“:45},{” endpoint“:” 127.0.0.1“,” id“:231,” identifiers“:[” steam:1100001065ace8a “,”许可证:“],”名称“:” unicornassassin“,” ping“:147},{”端点“:” 127.0.0.1“,” id“:166,”标识符“:[” steam:110000113334071“ ,“ license:”,“ xbl:2535468344890625”,“ live:1759222493808119”],“ name”:“ ItsQeeqo”,“ ping”:21},{“ endpoint”:“ 127.0.0.1”,“ id”:225 ,“标识符”:[“ steam:1100001119c6bdc”,“许可证:”],“名称”:“ Pizza Parker”,“ ping”:74},{“端点”:“ 127.0.0.1”,“ id”:238 ,“ identifiers”:[“ steam:110000117829c82”,“ license:”,“ xbl:2535428445779911”,“ live:914801662046383”],“ name”:“ Peraergold”,“ ping”:92},{“ endpoint”: “ 127.0.0.1”,“ id”:186,“标识符”:[“ steam:11000010582ce53”,“许可证:”],“名称”:“ Danni Dank”,“ ping”:41},{“端点”: “ 127.0.0.1”,“ id”:241,“标识符”:[“ steam:1100001097779e3”,“”,“ xbl:2533275032997544”,“活动:844428204995039”],“名称”:“ Tony J.”,“ ping“:203},{” endpoint“:” 127.0.0.1“,” id“:240,” identifiers“:[” steam:110000112553cd6“,” license:“,” xbl:2535471542971056“,” live:914801366308041“ ],” name“:” Lassen“,” ping“:60},{” endpoint“:” 127.0.0.1“,” id“:150,” identifiers“:[” steam:110000110d1cebc“,” license:“,” live: 844428721915290“],”名称“:” 5D | 155 Edward Johnson“,” ping“:47},{” endpoint“:” 127.0.0.1“,” id“:220,” identifiers“:[” steam:1100001186378bc“ ,“ license:”,“ xbl:2535439158130935”,“ live:1759221955842916”],“名称”:“ Sync”,“ ping”:81},{“ endpoint”:“ 127.0.0.1”,“ id”:215 ,“ identifiers”:[“ steam:11000010b2f22a8”,“许可证:”],“名称”:“ Thomas W. | CIV-335“,” ping“:50},{” endpoint“:” 127.0.0.1“,” id“:213,” identifiers“:[” steam:11000010b196656“,” license:“],” name“: “ Birac”,“ ping”:66}]

1 个答案:

答案 0 :(得分:0)

http_build_query函数的目的是创建一个URL编码的查询字符串,您的代码中似乎不需要这样做。您的代码从JSON字符串创建了一个对象,但是您将其视为一个数组,因此需要将json_decode()函数调用的第二个参数设置为 TRUE ,以返回一个数组。

// use the second parameter to create an associative array from the JSON
$players = json_decode($data, true);

假设JSON中返回了多个播放器,请以此为起点来构建表格行:

foreach($players as $player) {
  echo '<tr>'
  foreach($player as $key => $value) {
    echo '<td>' . $value . '</td>'
  }
  echo '</tr>'
}

如果您不想显示播放器数组中的某些元素,则可以使用array_keys()函数来获取键并指定要在代码中输出的字段。

您还应该记住,您的代码取决于外部资源,该资源可能并不总是可用。如果提供的URL不可用,并且您的代码应处理这种情况,则file_get_contents()函数调用将生成异常。

try {
  $data = file_get_contents($url);
  if ($data === false) {
    // Handle the error
  }
} catch (Exception $e) {
  // Handle the exception
}

更新:您提供的JSON数据与您要实现的数据不匹配。数组中的每个JSON项仅包含5个属性: endpoint id identifiers name 和< em> ping 。

以下PHP代码将解析提供的JSON文件,并显示一些我认为您正在寻找的信息。

$json = file_get_contents('./players.json');

$json_data = json_decode($json, true);

foreach ($json_data as $player_data) {
    // Initialise the steam id to an empty string in case one is not found
    $player_steam_id = "";
    // Find the steam id in the identifiers array
    if (array_key_exists("identifiers", $player_data)) {
        $steam_identifiers = [];
        foreach ($player_data["identifiers"] as $identifier_str)
            if (preg_match("/^steam:/i", $identifier_str, $m))
                $steam_identifiers[] = $identifier_str;
        if (!empty($steam_identifiers)) {
            $player_steam_id = $steam_identifiers[0];
        }
    }
    $player_id = $player_data["id"];
    $player_name = $player_data["name"];
    // Show what we have
    print( $player_id . " - " . $player_name . " - " . $player_steam_id . "\n" );
}