php - HTML表格结构未按正确顺序显示数据
问题描述
我有一个显示来自 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>
我希望只有三列。数据以正确的顺序显示
解决方案
尝试
<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>
<?php
foreach ($characters as $character)
{
?>
<tr>
<td>
<?php echo $character->name; ?>
</td>
<td>
<?php echo $character->phoneNumber; ?>
</td>
<td>
<?php echo $character->amount; ?>
</td>
</tr>
<?php }
?>
</tbody>
</table>
推荐阅读
- pymc3 - 如何在 pm.Deterministic() 中插入 for 循环以在 pymc3 模型中创建数组?
- vue.js - Vuetify 跨组件的网格:子项中的列可以作为父项中的列吗?
- ruby-on-rails - 为什么我可以在 Rails 6 中的 ActionMailer::MessageDelivery 上使用已删除的“交付”方法
- c# - 如何在 C# 中触发其他类中的按钮?
- html - 在 Android/iOS 的输入字段上禁用用户选择
- javascript - 我应该为多个任务创建多个网络工作者还是只需要 1 个网络工作者就足够了?
- firebase - 没有身份验证的 Firestore 安全规则
- database - 我无法在我的颤振应用程序中从 firebase 数据库中检索数据。我不知道我的代码有什么问题
- ssl - 谷歌云上的 KAFKA 负载均衡器在 SSL 端口上出错
- dart - 无法完成程序