【发布时间】:2019-10-14 19:55:15
【问题描述】:
我有一个显示来自 JSON 的数据的表。我能够在表格中显示数据,但它不能很好地呈现表格。
表格的结构被扭曲了。它不会按照我希望它显示的顺序显示数据。
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Name</th>
<th>Number</th>
<th>Amount</th>
</tr>
</thead>
<?php
$url = 'xxxxxxxxxxxxxxxxxxxxxx'; // path to your JSON file
//$url = 'data.json'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$characters = json_decode($data);
$i = 0;
?>
<tbody>
<tr>
<?php
foreach ($characters as $character)
{
?>
<td>
<?php echo $character->name; ?>
</td>
<td>
<?php echo $character->phoneNumber; ?>
</td>
<td>
<?php echo $character->amount; ?>
</td>
<?php }
?>
</tr>
</tbody>
</table>
我希望只有三列。数据以正确的顺序显示
【问题讨论】:
-
很难说不知道 1:JSON 是什么样子,2:
table和table-bordered类的 CSS 是什么样子,3:您是否使用 jQuery datatables插件(猜测是因为 id 是dataTable) - 任何这些都可能破坏布局
标签: php html json foreach html-table