0

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

{
  "response_status": {
    "status_code": 0,
    "status_message": [
      {
        "reg_number": "123",
        "company": "Company A",
        "office": "Orlando",
        "phone_number": "28122312345",
        "postal_address": "000",
        "postal_code": "000"
      },
      {
        "reg_number": "552",
        "company": "Company B",
        "office": "Miami",
        "phone_number": "28122312345",
        "postal_address": "000",
        "postal_code": "000"
      },
      {
       "reg_number": "154",
        "company": "Company C",
        "office": "San Francisco",
        "phone_number": "28122312345",
        "postal_address": "000",
        "postal_code": "000"
      }
    ]
  }
}

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

{
  "response_status": {
    "status_code": 0,
    "status_message": [
      {
        "reg_number": "123",
        "company": "Company A",
        "office": "Orlando",
        "phone_number": "28122312345",
        "postal_address": "000",
        "postal_code": "000"
      },
      {
        "reg_number": "552",
        "company": "Company B",
        "office": "Miami",
        "phone_number": "28122312345",
        "postal_address": "000",
        "postal_code": "000"
      },
      {
       "reg_number": "154",
        "company": "Company C",
        "office": "San Francisco",
        "phone_number": "28122312345",
        "postal_address": "000",
        "postal_code": "000"
      }
    ]
  }
}
I need the result to output html as 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>
    <tr>
       <td>123</td>
       <td>Company A</td>
       <td>San Francisco</td>
       <td>28122312345</td>
       <td>000</td>
       <td>000</td>
   </tr>
   <tr>
       <td>552</td>
       <td>Company B</td>
       <td>Miami</td>
       <td>28122312345</td>
       <td>000</td>
       <td>000</td>
   </tr>
   <tr>
       <td>154</td>
       <td>Company C</td>
       <td>San Francisco</td>
       <td>28122312345</td>
       <td>000</td>
       <td>000</td>
   </tr>
</tbody>
<table>

So far I have playing around with 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>

But its not working. Is there any workaround to make this working?

nick
  • 333
  • 1
  • 4
  • 15
  • Are you `json_decode`-ing the data? Have you tried use the second parameter of `json_decode` to return an associative array instead? What's the error that you're getting? – Scopey Apr 09 '17 at 22:56

1 Answers1

0

Everything seems fine, as @Scopey commented, only missing here is json_decode

    $json = file_get_contents('path/to/your/JSONfile.json');
    $data= json_decode($json);

If you have done this already, then nothing is wrong.

Rüzgar
  • 947
  • 9
  • 20