0

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,

3
  • 1
    Have you heard of a foreach loop? Commented Dec 7, 2016 at 14:03
  • U have to loop through each element and put it in each td tag. Commented Dec 7, 2016 at 14:03
  • this is object, not array, so instead of $data['name'], you do $data->name; Commented Dec 7, 2016 at 14:20

4 Answers 4

1
$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.

Sign up to request clarification or add additional context in comments.

Comments

0

You can print each value using foreach:

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

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

Comments

0

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>

Comments

0

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
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.