我有一个我正在制作的排行榜,但我想在页面顶部显示当前用户排名,我一直在努力使以下代码正常工作
<?
if(! defined('BASEPATH') ){ exit('Unable to view file.'); }
$sql = $db->Query("SELECT uid, SUM(`total_clicks`) AS `clicks` FROM `user_clicks` GROUP BY uid ORDER BY `clicks` DESC");
$tops = $db->FetchArrayAll($sql);
$j = 0;
foreach($tops as $top){
$j++;
$user = $db->QueryFetchArray("SELECT id,login,email,country,coins FROM `users` WHERE `id`='".$top['uid']."'");
?>
<?=$user['login']?>
<?}?>
这将返回一个很好的行中的所有用户名,我尝试了以下
<?=$user['top']?>
<?=$user['j']?>
我理解它与需要改变的foreach语句有关,但我也知道我错过了什么。
我想要实现的是Rank : 31
答案 0 :(得分:1)
您可以直接在查询中生成排名。然后你不需要循环来计算等级
SELECT uid,
SUM(`total_clicks`) AS `clicks`,
@rank := @rank + 1 as ranking
FROM `user_clicks`, (select @rank := 0) r
GROUP BY uid
ORDER BY `clicks` DESC