排行榜,与位置,处理相同的点和跳转到下一个


Leaderboard, with position, dealing with same points and skipping to next

我有一些代码,我计算用户在排行榜上的积分和位置,然后根据他们的积分/分数降序显示它们。

现在我已经完成了编号,所以它看起来像这样

    1
  • 2
  • 2
  • 3
  • 3
  • 3
  • 4

然而,我想使它更正确,并根据

之前的数字跳过位置

所以它应该是这样的

    1
  • 2
  • 2
  • 4
  • 4
  • 4
  • 7

我的SQL查询如下:

$query = $db->query("select a.user_id, b.username, sum(a.points) as points, c.paid, a.time_entered
from ".TABLE_PREFIX."mytipper_tips a
inner join ".TABLE_PREFIX."users b on a.user_id = b.uid
Inner join ".TABLE_PREFIX."mytipper_users c on b.uid = c.uid
where c.compID=".intval($comp)." and a.compID=".intval($comp)."
group by a.user_id order by points desc, username asc");`

,然后用下面的代码遍历它们:

//now we have the query we can iterate through the list of users
$position = 1;
while($result=$db->fetch_array($query)) {
    //check if it is a paid comp and if the user has paid, if so we only want to do this for the paid userds
    if($is_paid==1 && $result["paid"]==1)   {
        $display = "1";
    } else if($is_paid==1 && $result["paid"]!=1)    {
        $display = "0";
    } else if($is_paid==0)  {
        $display = "1";
    }
    if($display=="1")   {
        //set the table row for display
        if($row==2 || $row="")  {
            $row=1;
        }   else    {
            $row=2;
        }
        $username = htmlspecialchars_uni($result['username']);
        $user_id = intval($result["user_id"]);
        if($points==$result["points"])  {
            $position--;
        }
        $points = intval(($result["points"]));
        $leaderboard_link = mytipper_check_build_sef("misc.php?id=".intval($comp)."&user=".intval($user_id)."&mytipper=leaderboard_detail", $title);
        if($margin_leaderboard=="1")    {
            $margin = "(".htmlspecialchars_uni($result["actual_result"]).")";
        }   else {
            $margin="";
        }
        eval("'$leaderboard_rows .= '"".$templates->get("leaderboard_row")."'";");
        $position ++;
    }
}`

我不知道该怎么纠正那些数字

我现在做的方法是,我取这个位置并减去1,使它与之前的分数保持相同的数字。

但是我怎样才能从第2个跳到第4个呢?

如果这可以作为查询的一部分,即使我也会很高兴

也许用一个简单的计数器+存储最后的位置和分数?

<?php
    $i         = 1; // Item counter
    $lastScore = 0; // Last score
    $lastPos   = 0; // Last position
    foreach( $... ) {
       $myPosition = $i; // My position equals item counter
       // If last score equals this score, my position equals last position
       if( $lastScore > 0 && $myscore == $lastScore ) {
           $myPosition = $lastPos;
       }
       $lastScore = $myScore;
       $lastPos   = $myPosition 
        ++$i;
    }

从概念上讲,您应该通过保持领带数量的计数来处理这种情况,以便在执行循环时它应该工作:

int currentPositionToDisplay = 0;
int positionsToMoveToNext = 1;
int currentPositionScore = MAX_INT;
While(person in row)
{
    if(person.score < currentPositionScore)
    {
        currentPosition += positionsToMoveToNext;
        currentPositionScore = person.score;
        positionsToMoveToNext = 1;
    }else
    {
       positionsToMoveToNext++;
    }
    PRINT currentPositionToDisplay;
}

这允许你保存你跳过的项目的数量,然后在移动到下一个项目时添加这些项目。