Answer the question
In order to leave comments, you need to log in
How to output JSON to a table?
Good afternoon. You need to display the status of game servers on the site (we are talking about the multiplayer of the ETS2 game). The site has a PHP SDK. When downloading the archive, it contains 2 files. Here are the codes for both files.
sample.php
---------------
<?php
require_once("TruckMPApi.php");
// Setup api
$api = new TruckMPApi();
$servers = $api->GetServers();
var_dump($servers);
?>
---------------
TruckMPApi.php
---------------
<?php
class TruckMPApi
{
public function __construct()
{
}
public function GetServers()
{
return json_decode(@file_get_contents(" api.ets2mp.com/servers"));
}
}
?>
------------------------
Servers can be added at any time. That is, I see an option in parsing the answer into a table on the principle "until it ends"
An example of executing the sample.php file (more precisely, everything that produces)
--------------------
object(stdClass)#2 (2) { ["error"]=> string(5) "false" ["response"]=> array(5) { [0]=> object(stdClass)#3 (9) { ["id"]=> int(1) ["ip"]=> string(20) "1.eu.game.ets2mp.com" ["port"]=> int(42860) ["name"] => string(9) "Europe #1" ["shortname"]=> string(5) "EU #1" ["online"]=> bool(true) ["players"]=> int(1906) [ "maxplayers"]=> int(3500) ["speedlimiter"]=> int(1) } [1]=> object(stdClass)#4 (9) { ["id"]=> int(3) [" ip"]=> string(20) "1.us.game.ets2mp.com" ["port"]=> int(42850) ["name"]=> string(16) "United states #1" [" shortname"]=> string(5) "US #1" ["online"]=>bool(true) ["players"]=> int(49) ["maxplayers"]=> int(1500) ["speedlimiter"]=> int(1) } [2]=> object(stdClass)#5 ( 9) { ["id"]=> int(4) ["ip"]=> string(20) "2.eu.game.ets2mp.com" ["port"]=> int(42880) ["name "]=> string(9) "Europe #2" ["shortname"]=> string(5) "EU #2" ["online"]=> bool(true) ["players"]=> int(1449 ) ["maxplayers"]=> int(2300) ["speedlimiter"]=> int(0) } [3]=> object(stdClass)#6 (9) { ["id"]=> int(6) ["ip"]=> string(18) "1.asia.game.kat.pw" ["port"]=> int(42860) ["name"]=> string(7) "Asia #1" [ "shortname"]=> string(5) "AS #1" ["online"]=>bool(true) ["players"]=> int(13) ["maxplayers"]=> int(500) ["speedlimiter"]=> int(1) } [4]=> object(stdClass)#7 ( 9) { ["id"]=> int(7) ["ip"]=> string(20) "1.brazil.game.kat.pw" ["port"]=> int(42860) ["name "]=> string(16) "South America #1" ["shortname"]=> string(5) "SA #1" ["online"]=> bool(true) ["players"]=> int( 94) ["maxplayers"]=> int(750) ["speedlimiter"]=> int(1) } } }int(42860) ["name"]=> string(16) "South America #1" ["shortname"]=> string(5) "SA #1" ["online"]=> bool(true) [" players"]=> int(94) ["maxplayers"]=> int(750) ["speedlimiter"]=> int(1) } } }int(42860) ["name"]=> string(16) "South America #1" ["shortname"]=> string(5) "SA #1" ["online"]=> bool(true) [" players"]=> int(94) ["maxplayers"]=> int(750) ["speedlimiter"]=> int(1) } } }
-----------------------
The TruckMPApi.php file gives a blank screen in the output.
What data is there (I will explain using the example of 1 Euroserver.
Server address - 1.eu.game.ets2mp.com
Port - 42860
Name - Europe #1
Short name - EU #1
Online - true
Players on the server - 1906
Maximum players - 3500
Availability speed limiter - 1
I also need to display a plate of the following option:
short name - online - how many players are now / maximum players - speed limiter
At the same time, online displayed in the form of pictures:
online - moisat.ru/img/online.jpg
offline - moysat.ru /img/ofline.jpg
There is a speed limiter - moysat.ru/img/speedyes.jpg
There is no speed limiter - moysat.ru/img/speedno.jpg
Pictures size 20*20
----------------
I would be grateful for a tip on literature, good examples. If someone hints how to do it, I will be grateful too. Just with json (if it is) never worked. For the finished code - a huge thank you from me.
Answer the question
In order to leave comments, you need to log in
$api->GetServers(); returns an object , respectively:
sample.php
<?php
require_once ("TruckMPApi.php");
$api = new TruckMPApi();
$servers = $api->GetServers();
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<title>Пример</title>
</head>
<body>
<table>
<? foreach ($servers->response as $obj): ?>
<tr>
<td><?= $obj->name; ?></td>
<td><?= (($obj->online) ? 'online' : 'offline'); ?></td>
<td><?= $obj->players; ?>/<?= $obj->maxplayers; ?></td>
<td><?= $obj->speedlimiter; ?></td>
</tr>
<? endforeach; ?>
</table>
</body>
</html>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question