PHP - Simple Display JSON Data
Submitted by razormist on Saturday, March 9, 2019 - 19:25.
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.
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
- </div>
- </nav>
- <div class="col-md-3"></div>
- <div class="col-md-6 well">
- <h3 class="text-primary">PHP - Simple Display JSON Data</h3>
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-2"></div>
- <div class="col-md-8">
- <?php include 'display_json.php';?>
- <form method="POST" action="">
- <button class="btn btn-primary" name="display"><span class="glyphicon glyphicon-eye-open"></span> Show Data</button>
- </form>
- </div>
- </div>
- </body>
- </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
[
{
"firstname": "John",
"lastname": "Smith",
"age": "22",
"gender": "Male"
},
{
"firstname": "Claire",
"lastname": "Temple",
"age": "16",
"gender": "Female"
}
]
display_json.php
- <?php
- ?>
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th>Firstname</th>
- <th>Lastname</th>
- <th>Age</th>
- <th>Gender</th>
- </tr>
- </thead>
- <tbody>
- <?php
- $url = 'data.json';
- foreach($data as $member){
- ?>
- <tr>
- <td><?php echo $member->firstname?></td>
- <td><?php echo $member->lastname?></td>
- <td><?php echo $member->age?></td>
- <td><?php echo $member->gender?></td>
- </tr>
- <?php
- }
- ?>
- </tbody>
- </table>
- <?php
- }else{
- ?>
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- <th>Firstname</th>
- <th>Lastname</th>
- <th>Age</th>
- <th>Gender</th>
- </tr>
- </thead>
- <tbody>
- <tr>
- <td></td>
- <td></td>
- <td></td>
- <td></td>
- </tr>
- </tbody>
- </table>
- <?php
- }
- ?>
Add new comment
- 557 views