Wednesday, 1 June 2016

How to load data from json file in php


If you are looking for tutorial on how to load JSON file data on Webpage in PHP script. JSON (Java Script Object Notation) is very fast enhancing the very common data format that can be make asynchronous communication between browser and server. This type of Standard functions are present in PHP for creating, parsing and reading JSON format. Suppose if you are working on web services and in old days in web services project used XML data as their primary data source for transferring data but after the JSON appeared. Web services used JSON data format for exchanging data this is because it is lighter than xml data. Here I will use file_get_contents() php function for fetching data from JSON file. After fetching data from JSON file I want to convert that data to associative array, so for this I have used json_decode() function. This function will convert json string to php associative array.

Source Code

video.json


 [  
      {  
           "video_title":"Export MySQL data to Excel in PHP - PHP Tutorial"  
      },  
      {  
           "video_title":"Live Table Add Edit Delete using Ajax Jquery in PHP Mysql"  
      },  
      {  
           "video_title":"Make SEO Friendly or Clean Url in PHP using .htaccess"  
      },  
      {  
           "video_title":"How to Add Watertext or Watermark to an Image using PHP GD Library"  
      },  
      {  
           "video_title":"Create Simple Image using PHP"  
      },  
      {  
           "video_title":"How to check Multiple value exists in an Array in PHP"  
      },  
      {  
           "video_title":"How to merge two PHP JSON Array"  
      },  
      {  
           "video_title":"How To Insert Data Using Stored Procedure In Php Mysql"  
      },  
      {  
           "video_title":"How to check Username availability using php, Ajax, Jquery and Mysql"  
      },  
      {  
           "video_title":"Rename uploaded image in php with upload validation"  
      },  
      {  
           "video_title":"How to generate simple random password in php?"  
      },  
      {  
           "video_title":"Auto Refresh Div Content Using jQuery and AJAX"  
      },  
      {  
           "video_title":"Insert Update Delete using Stored Procedure in Mysql and PHP"  
      }  
 ]  

load_json_data.php


 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial | How to Load JSON Data in PHP</title>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
      </head>  
      <body>  
           <br />  
           <div class="container" style="width:500px;">  
                <h3 align="">How to Load JSON Data in PHP</h3><br />                 
                <div class="table-responsive">  
                     <table class="table table-bordered">  
                          <tr>  
                               <th>Video Title</th>  
                          </tr>  
                          <?php   
                          $data = file_get_contents("video.json");  
                          $data = json_decode($data, true);  
                          foreach($data as $row)  
                          {  
                               echo '<tr><td>'.$row["video_title"].'</td></tr>';  
                          }  
                          ?>  
                     </table>  
                </div>  
           </div>  
           <br />  
      </body>  
 </html>  

0 comments:

Post a Comment