yetteh
yetteh

Reputation: 13

Converting a JSON Output into a HTML Table

So I have a JSON output here https://squad-servers.com/api/?object=servers&element=detail&key=fupxq9hl1vkxb4yxhkggada7e0jz8p6w1

What I'd like to do is get this into a HTML Table to create my own status page as such, this is where I got with it.

<?php
$json=file_get_contents("https://squad-servers.com/api/?object=servers&element=detail&key=fupxq9hl1vkxb4yxhkggada7e0jz8p6w1");
$data =  json_decode($json);

print_r($data); 

?>

stdClass Object
(
    [id] => 2272
    [name] => [ZXD] Zulu X-Ray Delta EU/UK #2
    [address] => 164.132.202.16
    [port] => 7797
    [private] => 0
    [password] => 0
    [query_port] => 27175
    [location] => United Kingdom
    [hostname] =>  Zulu X-Ray Delta EU/UK #2
    [map] => Kokan AAS v1
    [is_online] => 0
    [players] => 0
    [maxplayers] => 72
    [version] => a-8.8.116.11628
    [platform] => windows
    [uptime] => 97
    [score] => 4
    [rank] => 81
    [votes] => 0
    [favorited] => 0
    [comments] => 0
    [url] => https://squad-servers.com/server/2272/
    [last_check] => December 7th, 2016 08:50 AM EST
    [last_online] => December 7th, 2016 07:25 AM EST
)

But how can I get this into a table, so I can echo / print out each part of the array?

Many thanks,

Upvotes: 0

Views: 171

Answers (4)

Lajos Arpad
Lajos Arpad

Reputation: 77063

json_decode has an optional second parameter determining whether you want to have an associated array. This parameter is false by default and therefore you get a standard object, which is much more difficult to handle. Instead, make sure that you have an associated array:

$data =  json_decode($json, true);
foreach ($data as $key => $value) {
    //Do something with $key and $value
}

Upvotes: 0

JustOnUnderMillions
JustOnUnderMillions

Reputation: 3795

$tbl = "<table><tr><th>".implode('</th><th>',array_keys((array)$data))."</th></tr>";
$tbl .= "<tr><td>".implode('</td><td>',(array)$data)."</td></tr></table>";
print $tbl;

...on way to rome.

$head=array();
$body=array();
foreach($data as $k=>$v){
  $head[]="<th>$k</th>";
  $body[]="<td>$v</td>";
}
print "<table><tr>".implode('',$head)."</tr><tr>".implode('',$body)."</tr></table>";

...another way.

(every used function can be found at php.net)

This will only work on your example. Not every json output can be easy printed into an html-table.

Upvotes: 1

Flak
Flak

Reputation: 2670

All you need is to call print_r($data->name); so echo $data->name with current api call.

if you have more than one result:

 <table>    
     <?php  foreach ($object as $value) {
            echo '<tr>';
           echo '<td>'.$value->name.'</td>';
           echo '<td>'.$value->address.'</td>';
           echo '<td>'.$value->hostname.'</td>';
           echo '<td>'.$value->uptime.'</td>';

           echo '</tr>';
        }
    }
?>
</table>

Upvotes: 0

Artem Ilchenko
Artem Ilchenko

Reputation: 1035

You can print each value using foreach:

foreach ($data as $key => $value) {
  echo $value;
}

http://php.net/manual/en/control-structures.foreach.php

Upvotes: 0

Related Questions