PHP - Simple Display JSON Data

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

In this tutorial we will create a Simple Display JSON Data using PHP. This code can display JSON data with one click in instance. The code itself will call the JSON object in the data.json file and convert it into a readable data to be able to view in the 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 - Simple Display JSON Data</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-2"></div>
  19. <div class="col-md-8">
  20. <?php include 'display_json.php';?>
  21. <form method="POST" action="">
  22. <button class="btn btn-primary" name="display"><span class="glyphicon glyphicon-eye-open"></span> Show Data</button>
  23. </form>
  24. </div>
  25. </div>
  26. </body>
  27. </html>

Creating the Main Function

This code contains the main function of the application. This code will convert the json object into a human readable data in the table. 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. "age": "22",
  6. "gender": "Male"
  7. },
  8. {
  9. "firstname": "Claire",
  10. "lastname": "Temple",
  11. "age": "16",
  12. "gender": "Female"
  13. }
  14. ]

display_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. <th>Gender</th>
  11. </tr>
  12. </thead>
  13. <tbody>
  14. <?php
  15. $url = 'data.json';
  16. $data = file_get_contents($url);
  17. $data = json_decode($data);
  18.  
  19. foreach($data as $member){
  20. ?>
  21. <tr>
  22. <td><?php echo $member->firstname?></td>
  23. <td><?php echo $member->lastname?></td>
  24. <td><?php echo $member->age?></td>
  25. <td><?php echo $member->gender?></td>
  26. </tr>
  27. <?php
  28. }
  29. ?>
  30. </tbody>
  31. </table>
  32. <?php
  33. }else{
  34. ?>
  35. <table class="table table-bordered">
  36. <thead class="alert-info">
  37. <tr>
  38. <th>Firstname</th>
  39. <th>Lastname</th>
  40. <th>Age</th>
  41. <th>Gender</th>
  42. </tr>
  43. </thead>
  44. <tbody>
  45. <tr>
  46. <td></td>
  47. <td></td>
  48. <td></td>
  49. <td></td>
  50. </tr>
  51. </tbody>
  52. </table>
  53. <?php
  54. }
  55. ?>

There you have it we successfully created Simple Display JSON Data 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.