我正在做一个学校管理项目。我已经得到了这门课的总分和平均分。我遇到的问题是如何在一年级、二年级等班级中对学生的排名。我有下面总分和平均分的代码。我不知道如何绕过定位。我将感谢在这里能得到的一切帮助。谢谢
<?php $select_subject = $this->db->get_where('subject',
array('class_id' => $class_id))->result_array();
foreach($select_subject as $key => $subject):?>
<tr>
<td><?php echo $subject['name'];?></td>
<?php $obtained_mark_query = $this->db->get_where('mark',
array('class_id' =>$class_id, 'exam_id' => $exam_id,
'subject_id' => $subject['subject_id'], 'student_id' => $student_id));
if($obtained_mark_query->num_rows() > 0){
$class_score_one = $obtained_mark_query->row()->class_score1;
$class_score_two = $obtained_mark_query->row()->class_score2;
$class_score_three = $obtained_mark_query->row()->class_score3;
$exam_score = $obtained_mark_query->row()->exam_score;
$total_score = $class_score_one + $class_score_two +
$class_score_three + $exam_score;
$average_score = $total_score/4;
} ?>
$studentAverages = $this->db
->select('students.name AS student_name, student_id, AVG((class_score1 + class_score2 + class_score3 + exam_score) / 4) as avg_total_score')
->where('class_id', 2) // Class ID to get rankings for
->join('students', 'students.id = mark.student_id', 'left')
->order_by('avg_total_score', 'DESC')
->group_by('student_id')
->get('mark')
->result_array();
$studentRankings = [];
foreach ($studentAverages as $k=>$average) {
$studentRankings[$average['student_id']] = $k+1;
}
使用上面的代码,您可以在$studentBankings数组中获得班级中每个学生的排名。例如,如果你想知道学生#2的排名(student_id=2(,只需使用以下代码:
$studentRankings[2]
http://sqlfiddle.com/#!9/c0769a/2