动态评分表

This is the simplified code for the scoreboard. What I am trying to build is a table that auto sorts itself by Total Points and assigns the rank of 1 for the lowest score and increments rank up from there until everyone has a rank. If 2 people have the same score the rank would be a T then the number.

<?php
$sql_score = $mysqli->query("SELECT * from event_scoreboard WHERE workoutid = '$work_out[$workout_counter]'");
    $rowcount1=mysqli_num_rows($sql_score);     
    $array = array();
    while($objscore = $sql_score->fetch_object()) { 
        $score = $objscore->score;

        $total_score += $score * 3; 
    }

    $values = array($total_score);
    $ordered_values = $values;
    echo rsort($ordered_values);

    foreach ($values as $key => $value) {
        foreach ($ordered_values as $ordered_key => $ordered_value) {
            if ($value === $ordered_value) {
                $key = $ordered_key;
                break;
            }                          
        }
        echo $value . '- Rank: ' . ((int) $key + 1) . '<br/>';
    }
?>

The test code below works fine, but how can we achieve the same output by inserting those outputs from the while condition into a multi-dimentional array ex. $values = array($total_score);

Right now, i'm getting this output below. It only displays the array index['0'] which is why the Rank is 1 in all output.

13087 - Rank: 1
11029 - Rank: 1
110359 - Rank: 1
17035 - Rank: 1
19702 - Rank: 1
1702 - Rank: 1
196 - Rank: 1
19 - Rank: 1
110266 - Rank: 1

Working example to display the ranking based on score (currently DESC but it needs to be in ASC order - lowest score would be the rank # 1)

$values = array();
$values[0] = 13389;
$values[1] = 71298;
$values[2] = 234;
$values[3] = 9069;
$values[4] = 9936;
$values[5] = 2673;
$values[6] = 234;
$values[7] = 234;
$values[8] = 11634;
$values[9] = 1470;

$ordered_values = $values;
    echo rsort($ordered_values);

foreach ($values as $key => $value) {
    foreach ($ordered_values as $ordered_key => $ordered_value) {
        if ($value === $ordered_value) {
            $key = $ordered_key;
            break;
        }                          
    }
    echo $value . '- Rank: ' . ((int) $key + 1) . '<br/>';
}

The syntax for using PHP's rsort() is as follows:

bool rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] )

So, executing the line echo rsort($ordered_values) will evaluate to true on succuss (echoed as 1) or false on failure (echoed 0).

If you print_r() your $ordered_values array, you'll notice that it is in fact sorted in reverse order:

//the output I got
Array ( [0] => 71298 [1] => 13389 [2] => 11634 [3] => 9936 [4] => 9069 [5] => 2673 [6] => 1470 [7] => 234 [8] => 234 [9] => 234 ) 

Meaning that the lowest ranked number comes in the end.

Note: I assume that you actually intended the opposite of the outcome you get from your code. Not sure why you won't use the opposite of rsort() i.e. sort() to achieve your desired result.

Changing from rsort() to sort() would produce the following result:

13389- Rank: 9
71298- Rank: 10
234- Rank: 1
9069- Rank: 6
9936- Rank: 7
2673- Rank: 5
234- Rank: 1
234- Rank: 1
11634- Rank: 8
1470- Rank: 4

I am not seeing why you need the complexity of the multi-dimensional array. Why not just use an asort on the $value array? That will sort the $value array by value from low to high and will keep the key associations.