dqwh0109 2015-10-14 14:56
浏览 93

动态评分表

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/>';
}
  • 写回答

2条回答 默认 最新

  • douzhi9635 2015-10-14 17:07
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?