【发布时间】:2018-02-06 15:27:12
【问题描述】:
我必须创建一个前 10 名的排行榜。我设法在游戏丢失后使用ajax 将数据插入数据库,并在游戏丢失后获取该数据。但我无法更改排行榜中的最后一个(№ 10)位置。如果结果不在前 10 中,则更改该结果而不是 № 10,并且位置应该是他在所有结果中的位置。
这是从数据库中获取数据:
<?php
require("db_connection.php");
$id = 0;
$sql = "SELECT * FROM Leaderboard ORDER BY score DESC LIMIT 10";
$result = $conn->query($sql);
$datas = array();
if ($result->num_rows > 0) {
while ($row = $result-> fetch_assoc()) {
$datas[] = $row;
$id = $id + 1;
echo "<tr><td>" . $id . "</td><td>" . $row['name'] . "</td><td>" . $row['score'] . "</td></tr>";
}
}
?>
这是 JS:
$.post($("#myForm").attr("action"), $("input.info").serializeArray(), function(info) {
console.log(info);
});
$("#Datas").load('./php/get.php');
这是 HTML:
<div id="Leaderboard">
<table id="Datas">
<tr>
<th>id</th>
<th>Name</th>
<th>Score</th>
</tr>
</table>
</div>
【问题讨论】: