Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 排行榜,有位置,处理相同的分数并跳到下一个_Php_Mysql_Ranking_Leaderboard - Fatal编程技术网

Php 排行榜,有位置,处理相同的分数并跳到下一个

Php 排行榜,有位置,处理相同的分数并跳到下一个,php,mysql,ranking,leaderboard,Php,Mysql,Ranking,Leaderboard,我有一些代码,我计算用户的分数和在排行榜上的位置,然后根据他们的分数/得分按降序显示 目前,我已经做了编号,所以它看起来像这样 一, 二, 二, 三, 三, 三, 四, 然而,我想让这更正确,并根据之前的数字跳过位置 所以它必须看起来像这样 一, 二, 二, 四, 四, 四, 七, 我的SQL查询如下: $query = $db->query("select a.user_id, b.username, sum(a.points) as points, c.paid, a.time

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

目前,我已经做了编号,所以它看起来像这样

  • 一,
  • 二,
  • 二,
  • 三,
  • 三,
  • 三,
  • 四,
然而,我想让这更正确,并根据之前的数字跳过位置

所以它必须看起来像这样

  • 一,
  • 二,
  • 二,
  • 四,
  • 四,
  • 四,
  • 七,
我的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 ++;
    }
}`
我没办法去纠正那些数字

我现在的做法是,我占据这个位置,然后减去一,使其与之前的分数保持一致

但是我该如何从2号跳到4号呢


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

从概念上讲,您应该通过计算领带的数量来处理这种情况,这样当您进行循环时,它应该工作:

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;
}
int currentPositionToDisplay=0;
int positionsToMoveToNext=1;
int currentPositionScore=最大值;
While(在行人员)
{
如果(person.score

这样,您就可以对跳过的项目数进行计数,然后在移动到下一个项目时添加这些项目。

是否可以使用一个简单的计数器+存储上一个位置和分数

<?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;
    }

阅读此帖子:。你已经得到了答案我不会给你一个更好的答案,因为我不确定没有SQLfiddle我能不能。这是我的想法,但既然它是一个循环,我该如何重置计数器才能工作。计数器的重置应该发生在“positionsToMoveToNext=1”的行中。一旦检测到差异,计数器将被使用并重置。