我的措辞可能不对,但基本上这就是我想做的:
- 在 HTML 表格中显示排行榜
- 排行榜中的每个标题都是一个链接(例如得分、时间、击杀数)
- 当每个链接被点击时,它会根据点击的链接对表格进行排序(例如,如果点击击杀,则会对显示移动击杀数的数据进行排序)
Here是我当前排行榜的链接,效果很好。
这是我到目前为止的代码:
<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>
然后在 php 中,根据 $_GET["sort"]
参数的值设置要排序的列。
$sort
的值是实际的数据库列标题。
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");
这不是最干净的代码,但它展示了想法。
祝你好运。
关于php - 在 SQL 中对排行榜进行排序并使用 PHP 在表中更新它的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18146857/