创建排行榜,我将如何显示排名/位置?

发布于 2024-11-29 20:15:49 字数 125 浏览 1 评论 0原文

我正在创建一个排行榜,它将显示以下内容:排名、用户名、分数

我目前有一个表格,它将显示 mysql 表中数据的用户名和分数,我只是想知道如何显示每个排名用户,数字 1 是得分最高的用户,然后递减。

谢谢!

I am creating a leaderboards which will display the following: Rank, Username, Score

I currently have the table to it will display Username and Score from the data in a mysql table, I am just wondering how would I go about displaying a rank for each user, number 1 being the user with the highest score then descending.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

捎一片雪花 2024-12-06 20:15:49

我建议阅读 PHP/MySQL

HTML 标题: 打开表格,创建标题

<table>
    <tr>
        <td>Rank</td>
        <td>User</td>
        <td>Score</td>
    </tr>

PHP: 为每个用户动态生成行

    <?php

        $result = mysql_query("SELECT user, score FROM leaderboard ORDER BY score DESC");
        $rank = 1;

        if (mysql_num_rows($result)) {
            while ($row = mysql_fetch_assoc($result)) {
                echo "<td>{$rank}</td>
                      <td>{$row['user']}</td>
                      <td>{$row['score']}</td>";

                $rank++;
            }
        }
    ?>

HTML 页脚: 需要关闭表格

</table>

I recommend reading up on PHP/MySQL.

HTML Header: Open your table, create your headers

<table>
    <tr>
        <td>Rank</td>
        <td>User</td>
        <td>Score</td>
    </tr>

PHP: Dynamically generate the rows for each user

    <?php

        $result = mysql_query("SELECT user, score FROM leaderboard ORDER BY score DESC");
        $rank = 1;

        if (mysql_num_rows($result)) {
            while ($row = mysql_fetch_assoc($result)) {
                echo "<td>{$rank}</td>
                      <td>{$row['user']}</td>
                      <td>{$row['score']}</td>";

                $rank++;
            }
        }
    ?>

HTML Footer: need to close the table

</table>
携君以终年 2024-12-06 20:15:49

你可以用 SQL 完成整个事情:

SET @row = 0;
SELECT
    @row := @row + 1 AS rank
    userName,
    score
FROM
    leaderboard
ORDER BY
    score DESC

You can do the whole thing in SQL:

SET @row = 0;
SELECT
    @row := @row + 1 AS rank
    userName,
    score
FROM
    leaderboard
ORDER BY
    score DESC
生生不灭 2024-12-06 20:15:49

你需要
1)获取您要排名的记录的分数
2)计算具有“更好”分数的记录数量(“更好”取决于您的比赛类型。篮球,分数越高越好。高尔夫,分数越低越好。)

所以,类似的

select records in order
for each record
    score = record.score
    rank = select count(*) + 1 from table where score_column is better than score
    display data
end for

问题是执行对任何大量数据进行 count(*) 的速度都很慢。但是您可以看到,一旦获得前两个不同分数的排名,您就可以确定代码中其余行的排名,而无需查询。但请记住:您可能需要考虑关系。

You need to
1) get the score of the record you're trying to rank
2) count the number of records with a "better" score ("better" is dependent on your type of game. Basketball, higher scores are better. Golf, lower scores are better.)

So, something like

select records in order
for each record
    score = record.score
    rank = select count(*) + 1 from table where score_column is better than score
    display data
end for

The problem is that performing the count(*) on any significant amount of data is slow. But you can see that once you have the rank of the first TWO differing scores, you can determine the rank of the remaining rows in your code without a query. But remember: you probably need to account for ties.

给不了的爱 2024-12-06 20:15:49

我同意

    $result = mysql_query("SELECT user, score FROM leaderboard ORDER BY score DESC");
    $rank = 1;

    if (mysql_num_rows($result)) {
        while ($row = mysql_fetch_assoc($result)) {
            echo "<td>{$rank}</td>
                  <td>{$row['user']}</td>
                  <td>{$row['score']}</td>";

            $rank++;
        }
    }
?>`

,但我对过程 mysql 有疑问,所以我选择了 OOP 版本,但我找不到很多文档。但如果 @Josh 的答案不适合您,这应该会给您相同的结果。

<?php
        $query = 'SELECT user, score FROM leaderboard ORDER BY score DESC';
        $result = $conn ->query($query);
        while ($row = $result->fetch_row()){
            echo"<td>{$row[0]}</td>
                <td>{$row[1]}</td>";
        }
        ?>

当然,如果您希望显示排名,您也可以像上面一样添加它。

I agree with

    $result = mysql_query("SELECT user, score FROM leaderboard ORDER BY score DESC");
    $rank = 1;

    if (mysql_num_rows($result)) {
        while ($row = mysql_fetch_assoc($result)) {
            echo "<td>{$rank}</td>
                  <td>{$row['user']}</td>
                  <td>{$row['score']}</td>";

            $rank++;
        }
    }
?>`

But I had an issue with procedural mysql so I opted for the OOP version that I couldn't find a lot of documentation on. But this should give you the same result if @Josh's answer isn't working for you.

<?php
        $query = 'SELECT user, score FROM leaderboard ORDER BY score DESC';
        $result = $conn ->query($query);
        while ($row = $result->fetch_row()){
            echo"<td>{$row[0]}</td>
                <td>{$row[1]}</td>";
        }
        ?>

And of course if you want the rank to show you would add this also like above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文