当他们的分数相等时,如何将排名分配给他们分享最高排名的学生? [英] How to assign rank to students to where they share the highest rank when their scores equal?

查看:46
本文介绍了当他们的分数相等时,如何将排名分配给他们分享最高排名的学生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有对学生进行排名的代码,它有效,但不是我想要的——如果两个或更多学生的 score 得分相同,它不会rank 他们两个在相同的 rank 中.我希望最终的排名是这样的 - 如果两个或更多学生 score 相同的结果,我希望他们在同一个地方 rank 像这样:

I have code that ranks students and it works, but not as I intended -- if two or more students score the same score it doesn't rank them both in the same rank. I want the final ranking to be like this - if two or more students score the same result, I want them to rank in the same place like this:

1. miki ==> 97.8
2. lisa ==> 96.1 
2. jack ==> 96.1
4. john ==> 90.7 

请注意,lisa 和 jack score 相同 (96.1),并且给了他们相同的 rank (2nd),因此,3号被跳过.约翰是第四名.

Note that lisa and jack score the same (96.1), and it gives them the same rank (2nd), and therefore, 3rd is skipped. John is 4th.

当前代码

$student = '10_E';
$first = mysql_query("SELECT * FROM rej_students where student_selected = '$student'");

$teacher = mysql_query("SELECT * FROM teachers WHERE tech_id = '1002'");
$teachers = mysql_fetch_array($teacher);

$avg = mysql_query("SELECT * FROM avgfct_10 order by AVGFCT_10 desc");

$ra = 0; //rank
while ($go = mysql_fetch_array($avg))
{
    if ($go['AVGFCT_10'] != $go['AVGFCT_10'])
    {
        $micky = $ra ++;
    }
    if ($go['AVGFCT_10'] == $go['AVGFCT_10'])
    {
        $micky = 'same';
    }
    echo "id = " . $go['STUDENT_ID'] . "<br> AVARANGE = " . $go['AVGFCT_10'] . "<br>RANK = " . $micky . "<br> Class = " . $teachers['tech_hclass'] . "<br><br>";
}

推荐答案

你需要两个计数器

  • 绝对计数器(始终为 1、2、3、4、5 等).
  • 排名计数器 - 从 1 开始计数,但如果分数相同,则不会更新.只要分数不同,它就会用 absolute counter 更新.

示例代码

$counter = 1; // init absolute counter
$rank = 1; // init rank counter

// initial "previous" score:
$prevScore = 0;
while ($go = mysql_fetch_array($avg))
{
    // get "current" score
    $score = $go['AVGFCT_10'];

    if ($prevScore != $score) // if previous & current scores differ
        $rank = $counter;
    // else //same // do nothing

    echo "Rank: {$rank}, Score: {$score}<br>";
    $counter ++; // always increment absolute counter

    //current score becomes previous score for next loop iteration
    $prevScore = $score;
}

输出:

Rank: 1, Score: 97.8
Rank: 2, Score: 96.1
Rank: 2, Score: 96.1
Rank: 4, Score: 90.7

这篇关于当他们的分数相等时,如何将排名分配给他们分享最高排名的学生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆