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 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>

Is there a way I can iterate through the JSON body and and automatically output the loop for all the fields between "status_message": []; using a php foreach loop to my table?

cosmoonot
  • 2,161
  • 3
  • 32
  • 38
nick
  • 333
  • 1
  • 4
  • 15
  • 1
    Possible duplicate of [How do I extract data from JSON with PHP?](http://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – user3942918 Apr 09 '17 at 23:35

2 Answers2

0

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>
MoeinPorkamel
  • 701
  • 5
  • 8
  • Trying to get property of non-object and Invalid argument supplied for foreach(). Have you tried the snippet yourself? – nick Apr 09 '17 at 22:21
  • @nick yes its works. maybe you didnt stored data in $data variable or your $data variable is array! tell us how you fetch data from api and send it to your view. – MoeinPorkamel Apr 09 '17 at 22:52
0

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

Sandeep Kothari
  • 405
  • 3
  • 6