I am sending a HTTP request to a Server and get a response with json format. I convert it to array using:
$response = file_get_contents($final_url);
$responseArray = json_decode($response, true);
Here is server response array($responseArray
).
Array (
[status] => OK
[result] => Array (
[0] => Array
(
[trainno] => 12151
[name] => SAMARSATA EXP
[cls] => 1A 2A 3A SL
[rundays] => F,Sa
[from] => BQA
[fromname] => Bankura
[dep] => 03.45
[to] => HWH
[toname] => Howrah Jn
[arr] => 08.25
[pantry] => 0
[type] => SUPERFAST
[datefrom] => 10-Apr-2015
[dateto] => 12-Apr-2020
[traveltime] => 04.40
)
[1] => Array
(
[trainno] => 12151
[name] => SAMARSATA EXP
[cls] => 1A 2A 3A SL
[rundays] => F,Sa
[from] => BQA
[fromname] => Bankura
[dep] => 03.45
[to] => HWH
[toname] => Howrah Jn
[arr] => 08.25
[pantry] => 0
[type] => SUPERFAST
[datefrom] => 10-Apr-2015
[dateto] => 12-Apr-2020
[traveltime] => 04.40
)
)
)
Now I want to print 'result' array in html table format if [status] =>OK. How I can do it?
Just add an if condition, if it satisfies, then iterate the said array, pointing to proper indices:
if($responseArray['status'] === 'OK') {
foreach($responseArray['result'] as $result) {
echo $result['name']; // and other indices
}
}
Printing it into a table markup is just a creating a normal table which includes the markup.
Super rough example:
<?php if($responseArray['status'] === 'OK'): ?>
<table>
<thead>
<tr>
<?php foreach($headers as $h): ?>
<th><?php echo $h; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach($responseArray['result'] as $row): ?>
<tr>
<?php foreach($row as $value); ?>
<td><?php echo $value; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>