How to Parse JSON into a HTML Table Using PHP

How can I parse JSON into a html table using PHP?

Ok first thing to do when getting data from an external source is to understand what is being returned.

So do

<?php
$json=file_get_contents("http://west.basketball.nl/db/json/stand.pl?szn_Naam=2014-2015&cmp_ID=373");
$data = json_decode($json);

print_r($data);

Result:

stdClass Object
(
[stand] => Array
(
[0] => stdClass Object
(
[afko] => Risne Stars HS 1
[ID] => 2091
[status] => Actief
[gespeeld] => 3
[percentage] => 100.0
[punten] => 6
[tegenscore] => 149
[eigenscore] => 191
[datum] => 2014-10-05
[saldo] => 42
[team] => Risne Stars Heren 1
[positie] => 1
)

[1] => stdClass Object
(
[afko] => D.B.V. Arriba HS 2
[ID] => 1813
[status] => Actief
[gespeeld] => 2
[percentage] => 0.0
[punten] => 0
[tegenscore] => 116
[eigenscore] => 102
[datum] => 2014-10-05
[saldo] => -14
[team] => D.B.V. Arriba Heren 2
[positie] => 10
)

[2] => stdClass Object
(
[afko] => The Valley Bucketeers HS 2
[ID] => 2430
[status] => Actief
[gespeeld] => 2
[percentage] => 0.0
[punten] => 0
[tegenscore] => 177
[eigenscore] => 70
[datum] => 2014-10-05
[saldo] => -107
[team] => The Valley Bucketeers Heren 2
[positie] => 11
)

[3] => stdClass Object
(
[afko] => Uitsmijters HS 2
[ID] => 2143
[status] => Actief
[gespeeld] => 2
[percentage] => 100.0
[punten] => 4
[tegenscore] => 79
[eigenscore] => 161
[datum] => 2014-10-05
[saldo] => 82
[team] => Uitsmijters Heren 2
[positie] => 2
)

[4] => stdClass Object
(
[afko] => Picker Reds HS 1
[ID] => 2056
[status] => Actief
[gespeeld] => 3
[percentage] => 66.7
[punten] => 4
[tegenscore] => 193
[eigenscore] => 184
[datum] => 2014-10-05
[saldo] => -9
[team] => Picker Reds Heren 1
[positie] => 3
)

[5] => stdClass Object
(
[afko] => Peatminers HS 2
[ID] => 6247
[status] => Actief
[gespeeld] => 1
[percentage] => 100.0
[punten] => 2
[tegenscore] => 36
[eigenscore] => 64
[datum] => 2014-10-05
[saldo] => 28
[team] => Peatminers Heren 2
[positie] => 4
)

[6] => stdClass Object
(
[afko] => Jolly Jumpers HS 1
[ID] => 1994
[status] => Actief
[gespeeld] => 2
[percentage] => 50.0
[punten] => 2
[tegenscore] => 103
[eigenscore] => 119
[datum] => 2014-10-05
[saldo] => 16
[team] => Jolly Jumpers Heren 1
[positie] => 5
)

[7] => stdClass Object
(
[afko] => TONEGO '65 HS 2
[ID] => 2120
[status] => Actief
[gespeeld] => 2
[percentage] => 50.0
[punten] => 2
[tegenscore] => 107
[eigenscore] => 122
[datum] => 2014-10-05
[saldo] => 15
[team] => TONEGO '65 Heren 2
[positie] => 6
)

[8] => stdClass Object
(
[afko] => Amical HS 2
[ID] => 1791
[status] => Actief
[gespeeld] => 3
[percentage] => 33.3
[punten] => 2
[tegenscore] => 180
[eigenscore] => 195
[datum] => 2014-10-05
[saldo] => 15
[team] => Amical Heren 2
[positie] => 7
)

[9] => stdClass Object
(
[afko] => S.V.Z.W. HS 2
[ID] => 5526
[status] => Actief
[gespeeld] => 3
[percentage] => 33.3
[punten] => 2
[tegenscore] => 174
[eigenscore] => 151
[datum] => 2014-10-05
[saldo] => -23
[team] => S.V.Z.W. Heren 2
[positie] => 8
)

[10] => stdClass Object
(
[afko] => Twente Buzzards HS 3
[ID] => 2294
[status] => Actief
[gespeeld] => 3
[percentage] => 33.3
[punten] => 2
[tegenscore] => 196
[eigenscore] => 151
[datum] => 2014-10-05
[saldo] => -45
[team] => Twente Buzzards Heren 3
[positie] => 9
)

)

[nummer] => OHS2C
[version] => 1.0
[aantal_teams] => 11
[id] => 373
[seizoen] => 2014-2015
[naam] => Oost Afdeling Heren Senioren 2e klasse C
[gewijzigd] => 2014-10-05 18:34:25
)

So now you know you are dealing with an OBJECT and not scalar values or an array.

So try this code:-

<?php
$json=file_get_contents("http://west.basketball.nl/db/json/stand.pl?szn_Naam=2014-2015&cmp_ID=373");
$data = json_decode($json);

if (count($data->stand)) {
// Open the table
echo "<table>";

// Cycle through the array
foreach ($data->stand as $idx => $stand) {

// Output a row
echo "<tr>";
echo "<td>$stand->afko</td>";
echo "<td>$stand->positie</td>";
echo "</tr>";
}

// Close the table
echo "</table>";
}
?>

How can i parse json to a html table using PHP?

You should parse your json data to php object than you should iterate your data like below;


$myData = <<<JSON

[
{"Shop_name":"minh",
"Shop_id":"916TCR",
"Address":"cdsasffafa",
"numbers":"4",
"mob_no":"9447722856"
},
{"Shop_name":"Chig",
"Shop_id":"CKGTCR",
"Address":"afagagg",
"numbers":"8",
"mob_no":"6767564532"
}
]

JSON;

$myObject = json_decode($myData);

?>
<table>
<tr>
<td>Shop Name</td>
<td>Shop ID</td>
<td>Address</td>
<td>Numbers</td>
<td>Mob No</td>
</tr>
<?PHP
foreach($myObject as $key=>$item)
{
?>
<tr>
<td><?PHP echo $item->Shop_name; ?></td>
<td><?PHP echo $item->Shop_id; ?></td>
<td><?PHP echo $item->Address; ?></td>
<td><?PHP echo $item->numbers; ?></td>
<td><?PHP echo $item->mob_no; ?></td>
</tr>
<?PHP
}
?>
</table>

A working example is here: http://ideone.com/IqZLMs

PS: You should fix this ” quote to "

Parsing json to html table in PHP

Your data for HTML table is inside data array key.
You should write:

foreach($array['data'] as $result){

or even better:

$array = json_decode($response, true);
$resultArray = isset($array['data']) ? $array['data'] : [];

echo '<table>';
foreach($resultArray as $result){
echo '<tr>';
echo '<td>'.(isset($result['id']) ? $result['id'] : '-') .'</td>';
echo '<td>'.(isset($result['attributes']['name']) ? $result['attributes']['name'] : '-').'</td>';
echo '<td>'.(isset($result['attributes']['description']) ? $result['attributes']['description'] : '-').'</td>';
echo '<td>'.(isset($result['attributes']['funded_year']) ? $result['attributes']['funded_year'] : '-').'</td>';
echo '</tr>';
}
echo '</table>';

http://php.net/manual/en/function.json-decode.php

How to parse HTML table to JSON in one JSONObject?

You need to add the counter to the key of the object

$json['item']['name' + $i] = strip_tags($name);
$json['item']['status' + $i] = strip_tags($status);
$i++

How to parse this JSON url with Authorization to HTML table/ list with php

Solution 1.
JSON_FORCE_OBJECT on encoding PHP array value, then each array element will be added to an index even though if input array doesn’t have any index.

So you can just use

$escaped = json_encode($data, JSON_FORCE_OBJECT);

and your code will print the table.

Solution 2.
Currently json_encode converting data to StdObject in which you can read data using an arrow symbol. So printing data in your table just use foreach function reading Vehicles object with the arrow symbol.

 /*Dynamically generating rows & columns*/
foreach ($escaped->Vehicles as $vehicle){
$temp .= "<tr>";
$temp .= "<td>" . $vehicle->Model . "</td>";
$temp .= "<td>" . $vehicle->Motor . "</td>";
$temp .= "<td>" . $vehicle->Propellant . "</td>";
$temp .= "</tr>";
}

PHP - Show JSON array in html table

this code works perfectly fine - just testetd it on a local machine

<?php
$json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}]';
$json_decoded = json_decode($json);
foreach($json_decoded as $result){
print_r($result);
}
?>

To output your stuff as table, use this:

<?php
$json = '[{"name":"xxx","phone":"123","email":"a@a.com"},{"name":"yyy","phone":"456","email":"b@a.com"},{"name":"zzz","phone":"678","email":"c@a.com"}]';
$json_decoded = json_decode($json);
echo '<table>';
foreach($json_decoded as $result){
echo '<tr>';
echo '<td>'.$result->name.'</td>';
echo '<td>'.$result->phone.'</td>';
echo '<td>'.$result->email.'</td>';
echo '</tr>';
}
echo '</table>';
?>

Make sure that your JSON string doesn't have any syntax errors... if yes the json_decode will fail and the foreach() loop throws an error.



Related Topics



Leave a reply



Submit