jeudi 19 janvier 2017

Ranking data according to its value from highest to lowest

I have a problem in my ranking formula.My code is doing fine if it has no identical GPA or value of the grade were arranged from highest to lowest.

How to check the same value and assign the same points?I've troubled with this since if index or count 1-2-3 are the same then the formula would
conflict if the situation is count 2-3 were the same.
As the grade become lower, the points is also decreasing.

Example 1:

Step -1.See GPA 91 is the highest grade which multiplied by 1.

Step -2.See GPA 90 is the 2nd highest grade which multiplied by 0.9.

Step -1.See GPA 88,88,88 are 3rd highest grade which multiplied by 0.8.So each total points should be the same.

Example 2: What if 0=>91,1=>91 with same points or 1=>90,2=>90,3=>90 were the same?i've tried my code but its not getting the right logic.

Hope anyone could help me out.

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



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2iXnhTZ
via IFTTT

Aucun commentaire:

Enregistrer un commentaire