[英]Sort total scores from highest to lowest
I have a table called 'Broadsheet'.我有一张名为“Broadsheet”的表格。 This is where all the subject total scores of students in a class are aggregated.
这是 class 中学生的所有科目总分汇总的地方。
sn![]() |
name![]() |
math![]() |
english![]() |
french![]() |
total![]() |
---|---|---|---|---|---|
1 ![]() |
mary![]() |
70 ![]() |
65 ![]() |
85 ![]() |
220 ![]() |
2 ![]() |
michael![]() |
90 ![]() |
70 ![]() |
95 ![]() |
255 ![]() |
3 ![]() |
sam![]() |
80 ![]() |
50 ![]() |
95 ![]() |
225 ![]() |
The total for each subject was gotten with this query使用此查询获得每个主题的总数
public function getTotalMarksForStudnets($subject_id,$student_id,$table,$totField){
$this->db->select($totField);
$this->db->where('subject_id', $subject_id);
$this->db->where('student_id', $student_id);
$q = $this->db->get($table);
if($q->num_rows() > 0){
return $q->row()->$totField;
}else{
return '0';
}
}
And displayed with this并以此显示
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;">
<?php
$totalMarks = $this->student_model->getTotalMarksForStudnets($tmrow['subject_id'],$stdName['pstudent_id'],$table,$totField);
$gtotal = $gtotal + $totalMarks;
if($totalMarks!=0){
$totSubjects = $totSubjects + 1;
}
echo $totalMarks;
?></td>
While the overall total was displayed with this虽然总的总数是用这个显示的
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo $gtotal;?></td>
Everything was going fine until I had to sort the table from the highest total score to the lowest.一切都很顺利,直到我不得不从最高总分到最低分对表格进行排序。 The table should now look like this:
该表现在应如下所示:
sn![]() |
name![]() |
math![]() |
english![]() |
french![]() |
total![]() |
---|---|---|---|---|---|
1 ![]() |
michael![]() |
90 ![]() |
70 ![]() |
95 ![]() |
255 ![]() |
2 ![]() |
sam![]() |
80 ![]() |
50 ![]() |
95 ![]() |
225 ![]() |
3 ![]() |
mary![]() |
70 ![]() |
65 ![]() |
85 ![]() |
220 ![]() |
Here, I get really stuck.在这里,我真的卡住了。 How do I go from here?
我如何从这里 go ?
okay, as suggested by @titi, I did a group_by query and came up with this:好的,正如@titi 所建议的,我做了一个 group_by 查询并想出了这个:
public function getOverallMarksForStudnets($subject_id,$student_id,$table,$totField){
$this->db->select_sum($totField);
$this->db->where('subject_id', $subject_id);
$this->db->where('student_id', $student_id);
$this->db->group_by('subject_id', $subject_id);
$this->db->group_by('student_id', $student_id);
$this->db->order_by($totField, 'DESC');
$q = $this->db->get($table);
if($q->num_rows() > 0){
return $q->row()->$totField;
}else{
return '0';
}
}
If this is correct, how do I display it?如果这是正确的,我该如何显示它?
After using this query,使用此查询后,
public function getTotalMarksForStudnets($subject_id,$student_id,$table,$totField){
$this->db->select_sum($totField);
$this->db->group_by( 'student_id', 'subject_id' );
$this->db->order_by( $totField , 'DESC');
$q = $this->db->get($table);
if($q->num_rows() > 0){
return $q->row()->$totField;
}else{
return '0';
}
}
I get this:我明白了:
sn![]() |
name![]() |
math![]() |
english![]() |
french![]() |
total![]() |
---|---|---|---|---|---|
1 ![]() |
mary![]() |
255 ![]() |
255 ![]() |
255 ![]() |
255 ![]() |
2 ![]() |
michael![]() |
255 ![]() |
255 ![]() |
255 ![]() |
255 ![]() |
3 ![]() |
sam![]() |
255 ![]() |
255 ![]() |
255 ![]() |
255 ![]() |
My database schema is like this:我的数据库架构是这样的:
CREATE TABLE `scores_primary` (
`id` int(20) NOT NULL,
`student_id` int(11) DEFAULT NULL,
`class_id` int(5) DEFAULT NULL,
`section_id` int(5) DEFAULT NULL,
`subject_id` int(11) DEFAULT NULL,
`session_id` int(11) DEFAULT NULL,
`ca1` int(11) DEFAULT NULL,
`ca2` int(11) DEFAULT NULL,
`ca3` int(11) DEFAULT NULL,
`ca4` int(11) DEFAULT NULL,
`ca5` float(10,1) DEFAULT NULL,
`ca6` float(10,1) DEFAULT NULL,
`project` int(11) DEFAULT NULL,
`affective` int(11) DEFAULT NULL,
`psychomotor` int(11) DEFAULT NULL,
`exam` int(11) DEFAULT NULL,
`tot_score` int(11) DEFAULT NULL,
`created_at` datetime NOT NULL DEFAULT current_timestamp(),
`modified_at` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
tot_score ($totField) is what is collected in this broadsheet table under the various subjects. tot_score ($totField) 是在这个大表中各个主题下收集的内容。 $totalmarks is sum total of all tot_score ($totField).
$totalmarks 是所有 tot_score ($totField) 的总和。 This is what I want to use to sort the scores.
这就是我想用来对分数进行排序的东西。
You can add an order by
statement in your query您可以在查询中添加
order by
语句
$this->db->order_by( $totField, 'DESC');
DESC
for descending or ASC
for ascending DESC
用于下降或ASC
用于上升
Edited已编辑
What about something like this:像这样的东西怎么样:
$this->db->select( SUM($totField) );
$this->db->group_by( 'student_id', 'subject_id' );
$this->db->order_by( $totField , 'DESC');
$q = $this->db->get($table);
The result will be an array with total score for each pair of student / subject, and ordered by total score.结果将是一个数组,其中包含每对学生/科目的总分,并按总分排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.