根据给定的分数生成位置

时间:2017-12-14 06:34:59

标签: php positioning

function SubjectPosition($term, $session, $subject, $class, $student_id, $category){
    $res = 'results_sec';
    $get_position = mysqli_query($mysqli, "SELECT student_id, (SUM(con_ass)+SUM(hwork)+SUM(test)+SUM(exam_score)) AS Total
    FROM `$res`
    WHERE `class_id` = '$class' 
    AND `subject_id` = '$subject' 
    AND `session_id` = '$session' 
    AND `term_id` = '$term'
    GROUP BY student_id
    ORDER BY Total DESC") or die(mysqli_error($mysqli));
    $post = 1;
    $temp_score = 0;
    while ($rs = $get_position -> fetch_assoc()){
        $stud_id = $rs['student_id'];
        $score = $rs['Total'];
        if($student_id == $stud_id && $temp_score <> $score){
            return $post;
        }
        else if($student_id == $stud_id && $temp_score == $score){
            return $post - 1;
        }   
        $temp_score = $score;
        $post = $post + 1;
    }
}

假设查询返回

Student_id          |        Total
-----------------------------------
SID/001             |       701       
SID/005             |       702
SID/007             |       702
SID/002             |       702
SID/003             |       655
SID/004             |       639

我有上面的代码,想要为结果生成位置。 我希望这个职位与下表相似

Student_id          |        Total     |    Position
----------------------------------------------------
SID/001             |       701        |    1
SID/005             |       702        |    2
SID/007             |       702        |    2
SID/002             |       702        |    2
SID/003             |       655        |    5
SID/004             |       639        |    6

我需要在上面的脚本中纠正什么。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

function SubjectPosition($term, $session, $subject, $class, $student_id, $category){
 $res = 'results_sec';
 $get_position = mysqli_query($mysqli, "SELECT student_id, (SUM(con_ass)+SUM(hwork)+SUM(test)+SUM(exam_score)) AS Total
 FROM `$res`
 WHERE `class_id` = '$class' 
 AND `subject_id` = '$subject' 
 AND `session_id` = '$session' 
 AND `term_id` = '$term'
 GROUP BY student_id
 ORDER BY Total DESC") or die(mysqli_error($mysqli));
 $post = 0;
 $temp_score = 0;$return_pos = 0;
 while ($rs = $get_position -> fetch_assoc()){
    $stud_id = $rs['student_id'];
    $score = $rs['Total'];
    $post = $post + 1;
    if($temp_score <> $score) {
       $return_pos = $post;
    }
    if($student_id == $stud_id {
       return $return_pos;
    }
    $temp_score = $score;
  }
}