2017-04-09 9 views
0

私はブートストラップデータ型を使用して、APIのデータを表に表示します。データは次のとおりです。phpを使用してjson配列をループし、htmlを出力します

{ 
    "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" 
     } 
    ] 
    } 
} 

私はブートストラップデータ型を使用して、APIのデータをテーブルに表示しています。

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

は、これまでのところ、私はこの

<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

データを 'json_decode 'していますか?代わりに 'json_decode'の第2引数を使って連想配列を返そうとしましたか?あなたが得ているエラーは何ですか? – Scopey

答えて

0

すべてが@Scopeyがコメントとしてあなたは、これはすでに、何が間違っていない行っている場合は、ここでしか見つからないこと

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

json_decodeで、罰金です。

関連する問題