How to load/display JSON File using PHP

Getting Started

I've used Bootstrap in this tutorial which is a CSS framework and is included in the downloadable of this tutorial but if you want, you may download Bootstrap using this link. Also, take note that I'll be using members.json that I have included in the downloadable of this tutorial.

Creating the Script

Lastly, we create the script to load/display the data in our JSON File. Create a new file, name it as index.php and paste the codes below.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.         <meta charset="utf-8">
  5.         <title>How to load/display JSON File using PHP</title>
  6.         <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <div class="container">
  10.         <h1 class="page-header text-center">Load/Display JSON File using PHP</h1>
  11.         <div class="row">
  12.                 <div class="col-sm-8 col-sm-offset-2">
  13.                         <table class="table table-bordered table-striped">
  14.                                 <thead>
  15.                                         <th>ID</th>
  16.                                         <th>Firstname</th>
  17.                                         <th>Lastname</th>
  18.                                         <th>Address</th>
  19.                                         <th>Gender</th>
  20.                                 </thead>
  21.                                 <tbody>
  22.                                         <?php
  23.                                                 $data = file_get_contents('members.json');
  24.                                                 $data = json_decode($data);
  25.                                                 foreach($data as $row){
  26.                                                         echo "
  27.                                                                 <tr>
  28.                                                                         <td>".$row->id."</td>
  29.                                                                         <td>".$row->firstname."</td>
  30.                                                                         <td>".$row->lastname."</td>
  31.                                                                         <td>".$row->address."</td>
  32.                                                                         <td>".$row->gender."</td>
  33.                                                                 </tr>
  34.                                                         ";
  35.                                                 }
  36.                                         ?>
  37.                                 </tbody>
  38.                         </table>
  39.                 </div>
  40.         </div>
  41. </div>
  42. </body>
  43. </html>
That ends this tutorial. Happy Coding :)

Comments

As always, you provide great information. Is there method to retrieve a single row (ID) using json? Let's say I only wish to display the row ID 5 using echo in page: "id": "5", "firstname": "cristine", "lastname": "demapanag", "address": "talisay", "gender": "Female" can that be done? Thanks!

First, create a new column for the view. Then add a new anchor tag in our table column with the href value as 'view.php?id=".$row->id."'. Then create a new file "view.php" and paste the codes below. id){ echo "ID: ".$row->id."
"; echo "Firstname: ".$row->firstname."
"; echo "Lastname: ".$row->lastname."
"; echo "Address: ".$row->address."
"; echo "Gender: ".$row->gender."
"; } } ?>

Add new comment