-8

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 

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.

Current code

$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>";
}
Miki
  • 157
  • 1
  • 8
  • 1
    It is better to include code here, because links might be removed.. Also, read the [tour](http://www.stackoverflow.com/tour) .. – Naruto Jun 28 '17 at 13:58
  • this is going to get downvoted faster than the bill for healthcare (sorry had to) - read the tour as @Naruto suggests and try again :) – treyBake Jun 28 '17 at 13:59
  • 2
    Please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) and [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) and how to create a [Minimal, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) – RiggsFolly Jun 28 '17 at 13:59
  • 3
    Please dont post pictures of code. Post the code itself as text – RiggsFolly Jun 28 '17 at 14:04
  • 3
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[this happens](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Jun 28 '17 at 14:05
  • The crux of the question, I think, is how to give students the same (highest scored) rank when they share the same numerical `result`, while maintaining the consecutive rank for students who don't have shared result – Dennis Jun 28 '17 at 14:11

1 Answers1

3

You need two counters

  • Absolute Counter (always 1, 2, 3, 4, 5, etc).
  • Rank Counter - it counts from 1 but if the scores are same, it does not update. As soon as scores are different, it updates with the absolute counter.

Sample Code

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

Output:

Rank: 1, Score: 97.8
Rank: 2, Score: 96.1
Rank: 2, Score: 96.1
Rank: 4, Score: 90.7
Dennis
  • 7,907
  • 11
  • 65
  • 115