PHP - Populate HTML Table WIth JSON Object

In this tutorial we will create a Populate HTML Table WIth JSON Object using PHP. This code will display JSON data in HTML table when user click the button. The code use PHP POST method to call a specific function that will call the JSON object data.json file and convert it into a readable object to be able to display in table. This is a user-friendly kind of program feel free to modify it. We will be using JSON as a data management to store user data, and it will act as a database storage. It is an open-standard file format that uses human-readable text to transmit data objects into the local server.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Populate HTML Table WIth JSON Object</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-6">
  18.                         <?php
  19.                                 $file = 'data.json';
  20.                                 $data = file_get_contents($file);
  21.                                 $json = json_decode($data);
  22.                                 echo"<pre>";
  23.                                 print_r($json);
  24.                                 echo"</pre>";
  25.                         ?>
  26.                         <form method="POST" action="">
  27.                                 <center><button class="btn btn-primary" name="display">Populate</button></center>
  28.                         </form>
  29.                 </div>
  30.                 <div class="col-md-6">
  31.                         <?php include 'populate_json.php';?>
  32.                 </div>
  33.         </div>
  34. </body>
  35. </html>

Creating the Main Function

This code contains the main function of the application. This code will display the json object in HTML table when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below. data.json [ { "firstname": "John", "lastname": "Smith", "gender": "Male" }, { "firstname": "Claire", "lastname": "Temple", "gender": "Female" } ] populate_json.php
  1. <?php
  2.         if(ISSET($_POST['display'])){
  3. ?>
  4. <table class="table table-bordered">
  5.         <thead class="alert-info">
  6.                 <tr>
  7.                         <th>Firstname</th>
  8.                         <th>Lastname</th>
  9.                         <th>Age</th>
  10.                 </tr>
  11.         </thead>
  12.         <tbody>
  13.                 <?php
  14.                         $url = 'data.json';
  15.                         $data = file_get_contents($url);
  16.                         $json = json_decode($data);
  17.                        
  18.                         foreach($json as $member){
  19.                 ?>
  20.                 <tr>
  21.                         <td><?php echo $member->firstname?></td>
  22.                         <td><?php echo $member->lastname?></td>
  23.                         <td><?php echo $member->gender?></td>
  24.                 </tr>
  25.                 <?php
  26.                         }
  27.                 ?>
  28.         </tbody>
  29. </table>
  30. <?php
  31.         }else{
  32. ?>
  33. <table class="table table-bordered">
  34.         <thead class="alert-info">
  35.                 <tr>
  36.                         <th>Firstname</th>
  37.                         <th>Lastname</th>
  38.                         <th>Age</th>
  39.                 </tr>
  40.         </thead>
  41.         <tbody>
  42.                 <tr>
  43.                         <td></td>
  44.                         <td></td>
  45.                         <td></td>
  46.                         <td></td>
  47.                 </tr>
  48.         </tbody>
  49. </table>
  50. <?php
  51.         }
  52. ?>
There you have it we successfully created Populate HTML Table WIth JSON Object using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment