Outputing JSON to an HTML table using PHP

后端 未结 2 1314
礼貌的吻别
礼貌的吻别 2021-01-27 23:45

I using bootstrap datatables to display data from an API in a table. Here is the data:

{
  \"response_status\": {
    \"status_code\": 0,
    \"status_message\":         


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

    Considering your data is stored in some variable say $your_data... Use json_decode($your_data,true) to get the data in array format... then loop the array

    0 讨论(0)
  • 2021-01-28 00:18

    Assume that your data is stored in $data variable, your code should be like this:

    <table>
    <thead>
       <tr>
           <th>Reg No</th>
           <th>Company</th>
           <th>Office</th>
           <th>Phone Number</th>
           <th>Postal Address</th>
           <th>Postal Code</th>
       </tr>
    </thead>
    <tbody>
        <?php foreach($data->response_status->status_message as $message) : ?>
        <tr>
           <td><?php echo $message->reg_number; ?></td>
           <td><?php echo $message->company; ?></td>
           <td><?php echo $message->office; ?></td>
           <td><?php echo $message->phone_number; ?></td>
           <td><?php echo $message->postal_address; ?></td>
           <td><?php echo $message->postal_code; ?></td>
       </tr>
       <?php endforeach; ?>
    </tbody>
    <table>
    
    0 讨论(0)
提交回复
热议问题