【问题标题】:Displaying data from JSON file in table using PHP使用 PHP 在表中显示 JSON 文件中的数据
【发布时间】:2015-01-11 02:34:18
【问题描述】:

请有人指出我哪里出错了。

我正在尝试使用 PHP 在 html 表中显示 JSON 数据文件(由 phpmyadmin 中的导出函数生成)。

这是 JSON 文件:

      /**
       Export to JSON plugin for PHPMyAdmin
       @version 0.1
       */

      [{"Player": "Shazu","Games Without Loss": 8}, {"Player": "Vlad","Games Without Loss": 7}]

这里是 PHP:

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"                               "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <link href="CSS/json.css" rel="stylesheet" type="text/css" />

      <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>Untitled Document</title>
      </head>

      <body>

      <thead>

          <tr>
              <th>Player</th>
              <th>Games Without Loss</th>
          </tr>
      </thead>

      <?php

      $json=file_get_contents("1314withoutloss.json");
      $data =  json_decode($json, true);


      foreach($data->results as $item) 
      {
              echo '<tr>';
                  echo '<td>'.$item['Player'].'</td>';
                  echo '<td>'.$item['Games Without Loss'].'</td>';
              echo '</tr>';
      }

      ?>

      </body>
      </html>

这是我在浏览器中得到的:

没有损失的玩家游戏
注意:尝试在第 32 行获取 C:\xampp\htdocs\Playersleague2\jsontest.php 中非对象的属性

警告:第 32 行 C:\xampp\htdocs\Playersleague2\jsontest.php 中为 foreach() 提供的参数无效

非常感谢您的帮助。

【问题讨论】:

  • 首先,您想将表格包装在 标记中。你的头的父母是身体标签。将其全部包装在一对表格标签中:)。此外,在您的头部下方,将您的表格行放在一个 tbody 中。
  • 其次,$data 上似乎没有 results 属性。请改用foreach ($data as $item)

标签: php html json phpmyadmin html-table


【解决方案1】:

试试这个,适应你的代码:

虚拟代码:

$json = '[{"Player": "Shazu","Games Without Loss": 8}, {"Player": "Vlad","Games Without Loss": 7}]';

$data = json_decode($json);
foreach ($data as $item)
{
echo $item->Player;

}

这是您的实际代码:

$data =  json_decode($json);


foreach($data as $item)
{
    echo '<tr>';
    echo '<td>'.$item->Player.'</td>';
    echo '<td>'.$item->{"Games Without Loss"}.'</td>';
    echo '</tr>';
}

通过使用json_decode($data, true),返回的对象将被转换为关联数组,因此您不能像在foreach ( ... ) 中那样使用对象运算符-&gt; 访问它们。无论如何,它只是$data,对象或数组值将在循环内获取(在这种情况下,如果确实有一个名为“result”的对象/数组键会有所不同)

编辑:

使用这个:

...
$json = file_get_contents('json.json');

$s = explode("*/",$json);
$data =  json_decode($s[1]);
...

请注意:会像这样工作,因为您的 json 文件有一些额外的注释行,而不是这个,您可以尝试调整 phpadmins 输出以摆脱文件顶部的注释块

【讨论】:

  • 感谢您的帮助。我已经使用了你的代码,它确实有效。但是,循环不是显示在两行中,而是全部显示在一行中。再说一遍,我希望能够指向一个 .json 文件,并且每次都指定该文件。它在浏览器中给出了这些错误:警告:在第 25 行的 C:\xampp\htdocs\Playersleague2\jsontest.php 中为 foreach() 提供的参数无效再次感谢您的建议。
  • 对于带有行的东西:在循环之前创建一个表,使用 table - thead-th-close thead-body 然后是循环,然后在循环之后关闭 tbody-close 表。然后它看起来像一张桌子。第二个可能有不同的可能性。您可以使用 var_dump($json); 检查文件是否可用在你调用 file_get_contents
  • 太棒了,表格问题被排序了,我觉得很愚蠢。 var 转储正在显示我的 json 数据,但是我收到警告:在第 30 行的 C:\xampp\htdocs\Playersleague2\jsontest.php 中为 foreach() 提供的参数无效。第 30 行是指 foreach($data as $item )。我现在就标记它。
  • 干杯!我会在 45 分钟后回到电脑前,再看看它。您在整个文件上方发布的行吗?如果没有,可以发一下吗?
  • 谢谢。是的,第 30 行是 foreach 行,这里是完整的语句 foreach($data as $item){ echo ""; echo "".$item->{"Player"}."";echo "".$item->{"Games Without Loss"}."";回声“ ”;}。并且文件的位置是 C:\xampp\htdocs\Playersleague2\1314withoutloss.json
【解决方案2】:

您没有在循环内正确访问 Object 键。当对象键有空格时,您可以访问它们:

 $item->{"Games Without Loss"}

另外,不建议使用带有空格的键。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-20
    • 2016-10-10
    • 2016-03-05
    • 1970-01-01
    • 2020-01-24
    相关资源
    最近更新 更多