• We just launched and are currently in beta. Join us as we build and grow the community.

How to Read JSON Data in PHP

crematedguy

Dependency Manager
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
70
Likes
132
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
If you are looking for a tutorial on How to Read JSON Data in PHP using jQuery then you are at the right place. Using jQuery you can construct less of source code but you can have more function. We all know that JSON is easy to understand, so let’s see how to display JSON data, and how we are going to disintegrate JSON into HTML Table, so let’s begin.

You will learn:

  • Creating a simple table in HTML.
  • Creating Database Connection.
  • Constructing PHP query.
  • A creating script where the JSON file and the Data table found.

Creating Table and Data

We are going to create MySQL Data for this tutorial and we will convert it into JSON format later, so kindly copy and paste this following data into your PHPMyAdmin.

  1. --
  2. -- Database: `json_tutorial`
  3. --

  4. -- --------------------------------------------------------

  5. --
  6. -- Table structure for table `tbl_posts`
  7. --

  8. CREATE

    TABLE

    `tbl_posts`

    (
  9. `postID`

    INT

    (

    11

    )

    NOT

    NULL

    ,
  10. `post_Title`

    VARCHAR

    (

    255

    )

    NOT

    NULL

    ,
  11. `post_Url`

    VARCHAR

    (

    255

    )

    NOT

    NULL
  12. )

    ENGINE=

    MyISAM DEFAULT

    CHARSET=

    latin1;

  13. --
  14. -- Dumping data for table `tbl_posts`
  15. --

  16. INSERT

    INTO

    `tbl_posts`

    (

    `postID`

    ,

    `post_Title`

    ,

    `post_Url`

    )

    VALUES
  17. (

    1

    ,

    'Date and Time of Last Visit'

    ,

    'http://www.sourcecodester.com/tutorials/javascript/9469/date-and-time-last-visit.html'

    )

    ,
  18. (

    2

    ,

    'Digital Clock using jQuery'

    ,

    'http://www.sourcecodester.com/tutorials/javascript/10606/digital-clock-using-jquery.html'

    )

    ,
  19. (

    3

    ,

    'Contact Form with Captcha Confirmation using PDO in PHP'

    ,

    'http://www.sourcecodester.com/tutor...-form-captcha-confirmation-using-pdo-php.html'

    )

    ,
  20. (

    4

    ,

    'Color Effect in Text using JavaScript'

    ,

    'http://www.sourcecodester.com/tutorials/javascript/10601/color-effect-text-using-javascript.html'

    )

    ,
  21. (

    5

    ,

    'Bootstrap Wizard Registration using PHP/MySQL'

    ,

    'http://www.sourcecodester.com/tutorials/php/10235/how-create-wizard-registration-using-php.html'

    )

    ,
  22. (

    6

    ,

    'How to Show and Hide Menu'

    ,

    'http://www.sourcecodester.com/tutorials/other/10206/how-show-and-hide-menu.html'

    )

    ,
  23. (

    7

    ,

    'If, Else, Else If Statement using JavaScript'

    ,

    'http://www.sourcecodester.com/tutor...-else-else-if-statement-using-javascript.html'

    )

    ,
  24. (

    8

    ,

    'How to Create Quick Search using PHP/MySQL with jQuery'

    ,

    'http://www.sourcecodester.com/tutorials/php/10592/how-create-quick-search-using-phpmysql-jquery.html'

    )

    ,
  25. (

    9

    ,

    'Confirmation Alert Box using JavaScript'

    ,

    'http://www.sourcecodester.com/tutor.../confirmation-alert-box-using-javascript.html'

    )

    ,
  26. (

    10

    ,

    'Members Information System using PHP/MySQL'

    ,

    'http://www.sourcecodester.com/tutorials/php/10586/members-information-system-using-phpmysql.html'

    )

    ;

JSON jQuery Script

This simple JSON script has three parameters, one is the URL, the second is our Data, and the Success use this if the request succeeds to the server.

$.getJSON

(

url,

data,

success)

;

Complete Source Code for JSON Script

This is the complete source code for JSON script, as you can see the code, it can calls PHP file and let’s disintegrate the JSON data into HTML table.

  1. <

    script type=

    "text/javascript"

    >
  2. $(

    document)

    .ready

    (

    function

    (

    )

    {

  3. var

    url=

    "getjson.php"

    ;

    // PHP File
  4. //var url="getposts.json"; // JSON File

  5. $.getJSON

    (

    url,

    function

    (

    data)

    {
  6. console.log

    (

    data)

    ;
  7. $.each

    (

    data.tutorials_post

    ,

    function

    (

    i,

    post)

    {
  8. var

    newRow =
  9. "<tr>"
  10. +

    "<td>"

    +

    post.post_Title

    +

    "</td>"
  11. +

    "<td><a href='"

    +

    post.post_Url

    +

    "'>Visit</a></td>"
  12. +

    "</tr>"

    ;
  13. $(

    newRow)

    .appendTo

    (

    "#data_OfJSON"

    )

    ;
  14. }

    )

    ;
  15. }

    )

    ;
  16. </

    script>

Simple HTML Table

In this table, we have id name of “data_OfJSON” that we are going to disintegrate and displayed the JSON data.

  1. <div

    class

    =

    "container"

    >
  2. <div

    class

    =

    "page-header"

    style

    =

    "text-align:center;"

    >
  3. <h2

    >
  4. <a

    href

    =

    "http://www.sourcecodester.com/"

    >
  5. How to Read JSON Data in PHP
  6. </

    a

    >
  7. </

    h2

    >
  8. </

    div

    >

  9. <div

    class

    =

    "table-responsive"

    >
  10. <table

    id

    =

    "data_OfJSON"

    class

    =

    "table table-bordered table-hover"

    >
  11. <tr

    >
  12. <th

    style

    =

    "color:blue;"

    >

    Tutorial Title</

    th

    >
  13. <th

    style

    =

    "color:blue;"

    >

    Tutorial Url</

    th

    >
  14. </

    tr

    >
  15. </

    table

    >
  16. </

    div

    >
  17. </

    div

    >

PHP Source Code Sample

This simple PHP source code which we have MySQL rows that converted to JSON format and we can convert it easily using json_encode

(

)

and we have the main root object it’s called “tutorials_post”.

  1. <?php
  2. require_once

    'db.php'

    ;

  3. $posts

    =

    array

    (

    )

    ;
  4. $query

    =

    "SELECT * FROM tbl_posts"

    ;

  5. $statement

    =

    $db_con

    ->

    prepare

    (

    $query

    )

    ;
  6. $statement

    ->

    execute

    (

    )

    ;

  7. while

    (

    $row

    =

    $statement

    ->

    fetch

    (

    PDO::

    FETCH_ASSOC

    )

    )

    {

  8. $posts

    [

    'tutorials_post'

    ]

    [

    ]

    =

    $row

    ;
  9. }

  10. echo

    json_encode

    (

    $posts

    )

    ;
  11. ?>

Database Connection

This is database connection to connect to the database, where we can get our data to displayed, and we have “json_tutorial” our database name and our table is “tbl_posts”.

  1. <?php

  2. $database_hostname

    =

    "localhost"

    ;
  3. $database_user

    =

    "root"

    ;
  4. $database_password

    =

    ""

    ;
  5. $database_name

    =

    "json_tutorial"

    ;

  6. try{

  7. $database_connection

    =

    new

    PDO(

    "mysql:host={$database_hostname}

    ;dbname={$database_name}

    "

    ,

    $database_user

    ,

    $database_password

    )

    ;

  8. }

    catch(

    PDOException $z

    )

    {

  9. die

    (

    $z

    ->

    getMessage

    (

    )

    )

    ;
  10. }
  11. ?>

Output

This is the result after disintegrating the JSON file to HTML table data as shown in the image below.

untitled_15.jpg


Kindly click the "Download Code" button below for full source code. Thank you very much.

Hope that this simple tutorial that I created may help you to your future projects. Also, for the beginners who want to learn basic of coding in PHP/MySQL, JSON, and jQuery.

If you are interested in programming, we have an example of programs that may help you even just in small ways.

Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

327,690

327,698

Top