Home > Uncategorized > Php script to for json to html table

Php script to for json to html table

A simple example of how to make a html table from a json list.


[code lang=”php”]
<?php
function json_to_table($file){
$section = file_get_contents($file);

$arr = json_decode($section);

echo "<table>";
echo "<tbody>";
echo "<tr>";
echo "<th>";
echo "Questions";
echo "</th>";
echo "</tr>";

foreach($arr as $k=>$v){
echo "<tr>";
echo "<td>";
echo "$v";
echo "</td>";
echo "</tr>";
}

echo "</tbody>";
echo "</table>";
}
?>
[/code]

Categories: Uncategorized Tags:
  1. March 29th, 2012 at 01:58 | #1

    Good stuff! I used a slightly modified version (I had nested JSON), but the general idea was the same. Thanks!

  2. DT
    November 15th, 2012 at 21:12 | #2

    Hey Joe,
    Do u mind sharing ur version of the code?
    Thx
    DT

  3. August 7th, 2014 at 23:39 | #3

    for nested json, you need to do something like this

    foreach ($arr as $k => $v){
    if (is_array($v)){
    json_to_table($v);
    }
    }

  1. No trackbacks yet.


5 × = twenty five