PHP - Populate HTML Table WIth JSON Object

Submitted by: 
Language: 
Visitors have accessed this post 115 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Populate HTML Table WIth JSON Object</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-6">
  19. <?php
  20. $file = 'data.json';
  21. $data = file_get_contents($file);
  22. $json = json_decode($data);
  23. echo"<pre>";
  24. print_r($json);
  25. echo"</pre>";
  26. ?>
  27. <form method="POST" action="">
  28. <center><button class="btn btn-primary" name="display">Populate</button></center>
  29. </form>
  30. </div>
  31. <div class="col-md-6">
  32. <?php include 'populate_json.php';?>
  33. </div>
  34. </div>
  35. </body>
  36. </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

  1. [
  2. {
  3. "firstname": "John",
  4. "lastname": "Smith",
  5. "gender": "Male"
  6. },
  7. {
  8. "firstname": "Claire",
  9. "lastname": "Temple",
  10. "gender": "Female"
  11. }
  12. ]

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.