根据数值从最高到最低对数据进行排名

时间:2017-01-19 16:58:34

标签: php laravel-5 ranking

我的排名公式存在问题。如果没有相同的GPA或者等级的值从最高到最低排列,我的代码就可以了。

如何检查相同的值并分配相同的点?我对此感到困扰,因为如果索引或计数1-2-3相同则公式将 如果情况计数2-3的冲突是相同的。 随着等级降低,分数也在下降。

示例1:

步骤-1。参见GPA 91是最高等级乘以1。

步骤-2。参见GPA 90是第二高等级,乘以0.9。

步骤-1。参见GPA 88,88,88是第三高等级,乘以0.8 总分数应该相同。

示例2: 如果0 => 91,1 => 91具有相同的点或1 => 90,2 => 90,3 => 90是相同的?我尝试了我的代码但它没有得到正确的逻辑。

希望有人能帮助我。

id of scholar
array:3 [▼
0 => 4
1 => 1
2 => 7
3 => 2
4 => 10
5 => 16
6 => 11
7 => 13
8 => 9
]
GPA of scholar
array:3 [▼
0 => 91
1 => 90
2 => 88
3 => 88
4 => 88
5 => 87
6 => 86
7 => 86
8 => 86
]

$scholar_id2 = Scholar::whereIn('scholar_id',$scholar_id)->whereBetween('scholar_GPA', [$ship_gpa_from, $ship_gpa_to])->orderBy('scholar_GPA','DESC')->get();
$id2 = [];
foreach ($scholar_id2 as $id) {
    array_push($id2, $id->scholar_id);
}
$count = 0;
foreach ($id2 as $item) {

    $ship_id2 = $ship_id;
    $crits = Criteria::where('scholar_id','=',$item)->where('ship_id','=',$ship_id2)->get();
    $id3 = [];
    foreach ($crits as $id) {
        array_push($id3, $id->id);
    }
    foreach ($id3 as $aw) {
        $crt = Criteria::find($aw);
            if ($count == 0) {
                $gpa = $ship_rate_two;
                $total_points_two = $gpa * 1;
                $crt->gpa = $total_points_two;
                $crt->save();
            }elseif($count == 1){
                $gpa = $ship_rate_two;
                $total_points_two = $gpa * 0.9;
                $crt->gpa = $total_points_two;
                $crt->save();
            }elseif($count == 2){
                $gpa = $ship_rate_two;
                $total_points_two = $gpa * 0.8;
                $crt->gpa = $total_points_two;
                $crt->save();
            }elseif($count == 3){
                $gpa = $ship_rate_two;
                $total_points_two = $gpa * 0.7;
                $crt->gpa = $total_points_two;
                $crt->save();
            }elseif($count == 4){
                $gpa = $ship_rate_two;
                $total_points_two = $gpa * 0.6;
                $crt->gpa = $total_points_two;
                $crt->save();
            }elseif($count == 5){
                $gpa = $ship_rate_two;
                $total_points_two = $gpa * 0.5;
                $crt->gpa = $total_points_two;
                $crt->save();
            }elseif($count == 6){
                $gpa = $ship_rate_two;
                $total_points_two = $gpa * 0.4;
                $crt->gpa = $total_points_two;
                $crt->save();
            }elseif($count == 7){
                $gpa = $ship_rate_two;
                $total_points_two = $gpa * 0.3;
                $crt->gpa = $total_points_two;
                $crt->save();
            }else{
                $gpa = $ship_rate_two;
                $total_points_two = $gpa * 0.2;
                $crt->gpa = $total_points_two;
                $crt->save();
            }
        }
        $count++;
    }

0 个答案:

没有答案