【发布时间】: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