在SQL中对排行榜排序并使用PHP在表中更新排行榜的最有效方法
我可能没有正确的措辞,但基本上我想做的是:在SQL中对排行榜排序并使用PHP在表中更新排行榜的最有效方法,php,mysql,html-table,Php,Mysql,Html Table,我可能没有正确的措辞,但基本上我想做的是: 在HTML表格中显示排行榜 排行榜中的每个标题都是一个链接(例如分数、时间、死亡) 单击每个链接时,它会根据单击的链接对表进行排序(例如,如果单击了kills,它会对顶部显示移动kills的数据进行排序) 是我当前排行榜的链接,效果很好 这是迄今为止我必须完成的代码: <div id="board"> <table border="1" cellspacing="0" cellpadding="2" width="620"
- 在HTML表格中显示排行榜
- 排行榜中的每个标题都是一个链接(例如分数、时间、死亡)
- 单击每个链接时,它会根据单击的链接对表进行排序(例如,如果单击了kills,它会对顶部显示移动kills的数据进行排序)
<div id="board">
<table border="1" cellspacing="0" cellpadding="2" width="620"><tbody>
<thead>
<tr>
<td>Name</td>
<td>Score</td> <---- WHEN CLICK = SORT IT BY SCORE
<td>Wave Reached</td> <---- WHEN CLICK = SORT IT BY WAVE
<td>Seconds Survived</td> <---- WHEN CLICK = SORT IT BY SECONDS
<td>Kills</td> <---- WHEN CLICK = SORT IT BY KILLS
<td>Deaths</td> <---- WHEN CLICK = SORT IT BY DEATHS
</tr>
</thead>
<tbody>
<?php
$connect = mysql_connect("localhost","localhost", "password");
if (!$connect) {
die(mysql_error());
}
mysql_select_db("staroids");
$results = mysql_query("SELECT name, score, wave, seconds, kills, deaths FROM scores ORDER BY score DESC LIMIT 10");
while($row = mysql_fetch_array($results)) {
$name = $row['name'];
$score = $row['score'];
$wave = $row['wave'];
$seconds = $row['seconds'];
$kills = $row['kills'];
$deaths = $row['deaths'];
?>
<tr>
<td><?php echo $name;?></td>
<td><?php echo $score;?></td>
<td><?php echo $wave;?></td>
<td><?php echo $seconds;?></td>
<td><?php echo $kills;?></td>
<td><?php echo $deaths;?></td>
</tr>
<?php
}
mysql_close($connect);
?>
</tbody>
</table>
</div>
名称
分数
我希望这能解释我打算做什么
上面的代码按降序显示分数。在HTML中按下链接(如JavaScript)以运行特定查询时,是否可以运行PHP脚本?在表头中,可以使用每个链接的不同参数链接到当前页面:
<tr>
<td><a href="currentpage.php?sort=name">Name</a></td>
<td><a href="currentpage.php?sort=score">Score</a></td>
...
</tr>
这不是最干净的代码,但它显示了想法。祝你好运。退房。如果您从服务器上以JSON(排序不重要)的形式获取所有数据,Javascript将负责排序。有没有一种方法可以在PHP服务器端进行排序?客户端可能会导致潜在的安全问题?问题是这会从数据库中选择前10名,其中可能有100条记录。。。我认为它需要为每个标题单击运行一个查询,然后运行相关的SQL Select查询。干杯!我一直在思考这些问题,我现在就来试试。我试过你说的,它会输出以下错误:当你点击标题()@smj2393时,你可以在这里查看错误。你应该用你想要使用的页面替换currentpage.php。例如leadboard.phpYea我不好,我没有提到这一点,我认为删除
currentpage.php
并将?sort=name
放在href中也可以。
if (isset($_GET["sort"])) {
if ($_GET["sort"] == "name")
$sort = "name";
else if ($_GET["sort"] == "score")
$sort = "score";
else
$sort = "score"; //default value
}
else
$sort = "score"; //default value
$results = mysql_query("SELECT name, score, wave, seconds, kills, deaths FROM scores ORDER BY ".$sort." DESC LIMIT 10");