php - 在 SQL 中对排行榜进行排序并使用 PHP 在表中更新它的最有效方法

标签 php mysql html-table

我的措辞可能不对,但基本上这就是我想做的:

  • 在 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/

相关文章:

javascript - 在鼠标悬停时为 td 着色

javascript - 添加新内容之前检查表格内容

php - 安装期间找不到 MediaWiki 的数据库驱动程序

php - 客户计算机上创建的 cookie 在哪里

php - MySql 半动态表名

mysql - 帮助使用 perl dbi 和 mysql 查询远程数据库

html - CSS : page-break-inside: avoid doesn't work in table row

php - laravel Eloquent 关系有很多查询

php - 我应该如何显示动态数据库信息?

mysql - 如何在全选查询中检查空值?