【问题标题】:Generating position based on a given score根据给定分数生成位置
【发布时间】:2017-12-14 15:19:23
【问题描述】:
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

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

【问题讨论】:

  • 你只想要学生的位置吗?同样在预期输出中,ID 为 SID/003 的学生是否应该排名第三?

标签: php positioning


【解决方案1】:
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;
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 2012-09-12
    • 2011-03-31
    • 1970-01-01
    • 2017-06-27
    相关资源
    最近更新 更多