创建排行榜,我将如何显示排名/位置?
我正在创建一个排行榜,它将显示以下内容:排名、用户名、分数
我目前有一个表格,它将显示 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议阅读 PHP/MySQL。
HTML 标题: 打开表格,创建标题
PHP: 为每个用户动态生成行
HTML 页脚: 需要关闭表格
I recommend reading up on PHP/MySQL.
HTML Header: Open your table, create your headers
PHP: Dynamically generate the rows for each user
HTML Footer: need to close the table
你可以用 SQL 完成整个事情:
You can do the whole thing in SQL:
你需要
1)获取您要排名的记录的分数
2)计算具有“更好”分数的记录数量(“更好”取决于您的比赛类型。篮球,分数越高越好。高尔夫,分数越低越好。)
所以,类似的
问题是执行对任何大量数据进行 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
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.
我同意
,但我对过程 mysql 有疑问,所以我选择了 OOP 版本,但我找不到很多文档。但如果 @Josh 的答案不适合您,这应该会给您相同的结果。
当然,如果您希望显示排名,您也可以像上面一样添加它。
I agree with
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.
And of course if you want the rank to show you would add this also like above.