drjyvoi734793 2018-12-05 05:40
浏览 122
已采纳

Json数据到表格上的表格

I have searched for weeks for an answer to this I have json data that I need to echo to the page and to auto fill a form based on the button pushed next to that name. here is a list of code that I have 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; ?>

Gives me this

https://gyazo.com/e72aee4c977e9226f2de3a2053f89c2f

and I want it to auto fill a form with the players name.

or something like this

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

}

I cant seem to wrap my head around this

Json Data

[{"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","live:1899947295907508"],"name":"[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"],"name":"Filippen","ping":40},{"endpoint":"127.0.0.1","id":202,"identifiers":["steam:110000134bddbd7","license:","xbl:2535453098180945","live:914802201806712"],"name":"VAC_brotha","ping":44},{"endpoint":"127.0.0.1","id":243,"identifiers":["steam:1100001351ef803","license:","xbl:2533274991210897","live:985156970449557"],"name":"Allan [S-662]","ping":177},{"endpoint":"127.0.0.1","id":218,"identifiers":["steam:11000010ad8b0bf","license:","xbl:2533274880694377","live:1759221802890599"],"name":"Katie Kat","ping":166},{"endpoint":"127.0.0.1","id":184,"identifiers":["steam:11000010be231cc","license:","xbl:2533275012889196","live:1759222203382981"],"name":"High Budget Trucker T-69","ping":55},{"endpoint":"127.0.0.1","id":244,"identifiers":["steam:110000115f03632","license:"],"name":"John Adams","ping":72},{"endpoint":"127.0.0.1","id":223,"identifiers":["steam:11000010bb69ddd","license:","live:1899943264922053"],"name":"[S853] Johnathen [1Z13]","ping":45},{"endpoint":"127.0.0.1","id":216,"identifiers":["steam:11000011cb29587","license:"],"name":"Steve","ping":198},{"endpoint":"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,"identifiers":["steam:1100001359fd857","license:"],"name":"[S-894] EpicDogePlayzYT","ping":183},{"endpoint":"127.0.0.1","id":236,"identifiers":["steam:11000010d29c185","license:"],"name":"Night","ping":24},{"endpoint":"127.0.0.1","id":193,"identifiers":["steam:1100001183e1fa8","license:"],"name":"J.W","ping":201},{"endpoint":"127.0.0.1","id":219,"identifiers":["steam:11000013559122f","license:"],"name":"[S-885]Damien.B","ping":61},{"endpoint":"127.0.0.1","id":187,"identifiers":["steam:110000108413e7a","license:","xbl:2535423985280873","live:985156647647394"],"name":"Purple Deathh","ping":18},{"endpoint":"127.0.0.1","id":130,"identifiers":["steam:110000113e184ad","license:","xbl:2535441445938947","live:844428429722616"],"name":"JamiDoesStuff","ping":23},{"endpoint":"127.0.0.1","id":205,"identifiers":["steam:11000011a84d2dc","license:"],"name":"Johnny","ping":45},{"endpoint":"127.0.0.1","id":231,"identifiers":["steam:1100001065ace8a","license:"],"name":"unicornassassin","ping":147},{"endpoint":"127.0.0.1","id":166,"identifiers":["steam:110000113334071","license:","xbl:2535468344890625","live:1759222493808119"],"name":"ItsQeeqo","ping":21},{"endpoint":"127.0.0.1","id":225,"identifiers":["steam:1100001119c6bdc","license:"],"name":"Pizza Parker","ping":74},{"endpoint":"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,"identifiers":["steam:11000010582ce53","license:"],"name":"Danni Dank","ping":41},{"endpoint":"127.0.0.1","id":241,"identifiers":["steam:1100001097779e3","","xbl:2533275032997544","live:844428204995039"],"name":"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"],"name":"5D|155 Edward Johnson","ping":47},{"endpoint":"127.0.0.1","id":220,"identifiers":["steam:1100001186378bc","license:","xbl:2535439158130935","live:1759221955842916"],"name":"Sync","ping":81},{"endpoint":"127.0.0.1","id":215,"identifiers":["steam:11000010b2f22a8","license:"],"name":"Thomas W. | CIV-335","ping":50},{"endpoint":"127.0.0.1","id":213,"identifiers":["steam:11000010b196656","license:"],"name":"Birac","ping":66}]

</div>
  • 写回答

1条回答 默认 最新

  • dongxu8533486 2018-12-05 06:44
    关注

    The purpose of the http_build_query function is to create a URL-encoded query string, there seems to be no need for this in your code. Your code creates an object from the JSON string but you are treating it like it's an array, you need to set the second parameter of the json_decode() function call to TRUE to have an array returned.

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

    Assuming there is more than one player returned in the JSON use this as a starting point to build your table rows:

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

    If there are elements of the player array which you don't want to display then you can use the array_keys() function to get the keys and specify the fields to output in your code.

    You should also keep in mind that your code is dependant on an external resource which may not always be available. The file_get_contents() function call will generate an exception if the provided URL is not available and your code should handle this scenario.

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

    UPDATE: The JSON data you have provided doesn't match up with what you are trying to achieve. Each of the JSON items in the array only contains 5 properties: endpoint, id, identifiers, name & ping.

    The following PHP code will parse the provided JSON file and display some of the information that I think you are looking for.

    $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 . "
    " );
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 php的curl不支持http2
  • ¥15 为什么我按照电路图做出的仿真和实物都不能使用
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多