php create dynamic html table

I need to create a dynamic html table using PHP after parsing a JSON file. I need this column structure for my table; Name | Status | Age | Count | Progress | Bad

How can i create a new row for every 'record' parsed from the JSON file (i can create a tab separated string).An additional difficulty is that some 'records' only contain data for the column 'Name' and others for all columns.

So my question is how to add a row dynamically to the table and fill the right column ? (The key form the JSON file is the column header)

example of json format:

{ 
"John":     {"status":"Wait" }, 
"Jennifer": {"status":"Active" }, 
"James":    {"status":"Active","age":56,"count":10,"progress":0.0029857,"bad":0} 
}

Answers


Something like this would work:

$data = json_decode($json_string);
$columns = array();

echo "<table><tbody>";
foreach ($data as $name => $values) {
    echo "<tr><td>$name</td>";
    foreach ($values as $k => $v) {
        echo "<td>$v</td>";
        $columns[$k] = $k;
    }
    echo "</tr>";
}
echo "</tbody><thead><tr><th>name</th>";
foreach ($columns as $column) {
    echo "<th>$column</th>";
}
echo "</thead></table>"

Need Your Help

jQuery is animated on multiple elements, which is better?

jquery animation elements animated

I am using jQuerys animate function on a plugin, on many elements simulatensly with different durations for each element. I want to know if any animation is running or if there is no animation at a...

How is it specified what is the first page to show in this Spring MVC demo application?

java spring spring-mvc

I am studtyin for the Spring Core certification and I have a doubt related Spring MVC use.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.