How to insert json data in to table?

后端 未结 2 1632
野的像风
野的像风 2021-01-26 00:29

I have written php code to generate json data, Now We have to display this json data into table. My php code is able to generate data but not able able to insert into ta

相关标签:
2条回答
  • 2021-01-26 00:54

    This is a way to work this with JavaScript

         function fetchData1(){              
                        $(".data-contacts1-js tbody").empty();
                        $.get("http://localhost/service/newJobs.php", function(data) {
                        data=JSON.parse(data); //if the server returns text instead JSON object                       
                         for(var i in data){
                             var tr=$("<tr></tr>");
                             tr.append("<td>" + data[i].cust_name + "</td>" +
                                    "<td>" + data[i].cust_mobile + "</td>" +
                                    "<td>" + data[i].cust_email + "</td>" +
                                    "<td>" + data[i].cust_address + "</td>");
                             $(".data-contacts1-js tbody").append(tr);
                           }
                        });
                    }  
    
                     $(document).ready(function(){
                          $(".data-contacts1-js tbody").empty();
                        $('#fetchContacts1').click(function() {
                             fetchData1();
                        });
                    });
    

    On server script (php) add in first row

    header('Content-type: application/json');
    
    0 讨论(0)
  • 2021-01-26 01:02

    isn't php an option to print the table? If yes you can just echo table rows in the foreach loop and that's it:

    while( $row = mysql_fetch_array( $result ) ) {
      $retVal[] = $row;
    }
    

    becomes something like

    while( $row = mysql_fetch_array( $result ) ) {
      $table_row .= "
         <tr>
            <td>$row['cust_name']</td>
            <td>$row['cust_mobile']</td>
            <td>$row['cust_email']</td>
            <td>$row['cust_address']</td>
         </tr>";
    }
    
    0 讨论(0)
提交回复
热议问题