Click here to Skip to main content
15,891,654 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have been trying to read my json data from json file and displaying in my application using php. Although i have managed to display my json data in my application but i want to display it in tables so that it looks nice.

What I have tried:

here's the table code block
<table class="table" id="allM">
  <thead class="thead-light">
    <tr>
      <th scope="col">ID</th>
      <th scope="col">PhoneNumber</th>
      <th scope="col">Body</th>
      <th scope="col">Date</th>
      <th scope="col">IsRead</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

here's the php code block
<?php
    $jsondata = file_get_contents("json_response.json");
    $json = json_decode($jsondata,true);
    $output = "<ul>";
    foreach($json["Messages"] as $messages)
    {
        $output .= "".$messages["id"]."";
        $output .= "<li>".$messages["phonenumber"]."</li>";
        $output .= "<li>".$messages["body"]."</li>";
        $output .= "<li>".$messages["M_date"]."</li>";
        $output .= "<li>".$messages["isRead"]."</li> <br/>";
        
    }
    $output .= "</ul>";
    echo $output;
    
    ?>

any suggestions would be great!
Posted
Updated 30-Jul-20 1:05am
v2

1 solution

You would need to assign identified to each table column such that you can access it. Further, given jsondata is fetched from outside, you need to populate the table dynamically (to get the right number of rows)

Try something like below:
JavaScript
var el_up = document.getElementById("GFG_UP"); 
          
        var list = [ 
            { "col_1": "val_11", "col_3": "val_13" }, 
            { "col_2": "val_22", "col_3": "val_23" }, 
            { "col_1": "val_31", "col_3": "val_33" } 
        ]; 
          
        el_up.innerHTML = "Click on the button to create " 
                +   "the table from the JSON data.<br><br>" 
                + JSON.stringify(list[0]) + "<br>"  
                + JSON.stringify(list[1]) + "<br>" 
                + JSON.stringify(list[2]);    
              
        function constructTable(selector) { 
              
            // Getting the all column names 
            var cols = Headers(list, selector);   
   
            // Traversing the JSON data 
            for (var i = 0; i < list.length; i++) { 
                var row = $('<tr/>');    
                for (var colIndex = 0; colIndex < cols.length; colIndex++) 
                { 
                    var val = list[i][cols[colIndex]]; 
                      
                    // If there is any key, which is matching 
                    // with the column name 
                    if (val == null) val = "";   
                        row.append($('<td/>').html(val)); 
                } 
                  
                // Adding each row to the table 
                $(selector).append(row); 
            } 
        } 
          
        function Headers(list, selector) { 
            var columns = []; 
            var header = $('<tr/>'); 
              
            for (var i = 0; i < list.length; i++) { 
                var row = list[i]; 
                  
                for (var k in row) { 
                    if ($.inArray(k, columns) == -1) { 
                        columns.push(k); 
                          
                        // Creating the header 
                        header.append($('<th/>').html(k)); 
                    } 
                } 
            } 
              
            // Appending the header to the table 
            $(selector).append(header); 
                return columns; 
        }

Refer: How to convert JSON data to a html table using JavaScript/jQuery ? - GeeksforGeeks[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900