我有一个游戏,在线分数存储在免费服务器上,如www.sitename.subdomain.com/globalscore.txt。
分数存储在json对象中,该对象具有json对象数组,每个对象包含播放器的名称,日期和分数。 总共有20个分数,并且总是排序。
实施例
[{"scor":4290,"data":"17\/05\/14 15:30:58","nume":"MyName"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"},{"data":"-","scor":"1","nume":"Fainosag"}]
我想创建一个页面,在一个简单的表格中显示分数,例如
<!DOCTYPE html>
<html>
<body>
<table border="1" style="width:300px">
<tr>
<td>Position</td>
<td>Name</td>
<td>Score</td>
</tr>
<tr>
<td>1</td>
<td>MyName</td>
<td>4290</td>
</tr>
<tr>
<td>2</td>
<td>Fainosag</td>
<td>1</td>
</tr>
</table>
</body>
</html>
当然,所有20行。 我怎么能这样做?
答案 0 :(得分:1)
可能是这样的:
我无法测试此代码。
$json = file_get_contents("http://blabla.com/myJson.txt");
$scores = json_decode($json, true);
echo "<table>";
echo "<thead><td>Position</td><td>Name</td><td>Score</td></thead>";
$i = 0;
foreach($scores as $score){
$i++;
echo "<tr><td>$i</td><td>".$score['nume']."</td><td>".$score['scor']."</td></tr>";
}
echo "</table>";
答案 1 :(得分:1)
如果您正在使用PHP在服务器端使用json对象,那么:
$content = file_get_content("www.sitename.subdomain.com/globalscore.txt");
$data = json_decode($content);
//loop on $data and print table $rows
foreach($data as $item) {
print '<tr>'. '<td>' .$item->scor.'</td>' .'</tr>';
}
答案 2 :(得分:1)
<html>
<head>
<script>
$(document).ready(function(){
// Fetch json object from server with AJAX and call updateTable(with json Object)
function updateTable(jsonObj) {
$(jsonObj).each(function(index, element){
$('#scores').append('<tr><td> '+element[0]+' </td> <td> '+element[1]+' </td> <td> '+element[2]+' </td></tr>');
})
}
});
</script>
<head>
<body>
.
.
<table id='scores' border="1">
<tr>
<td>Position</td>
<td>Name</td>
<td>Score</td>
</tr>
<table>
.
.
</body>
</html>