【问题标题】:Sum of students' total scores for each subject学生各科目总分之和
【发布时间】:2021-12-23 10:40:56
【问题描述】:

如何获得各学科学生总分的总和?

成绩表

student_id class_id section_id subject_id test1 test2 total
1 3 1 7 5 6 11
2 3 1 7 8 9 17
3 3 1 7 4 9 13
4 3 1 7 6 3 9
4 3 1 8 7 7 14

从上表中,我想要各个科目的所有学生总分的总和。 即 subject_id 7 中 id 为 1、2、3 和 4 的学生的总分分别为 11、17、13 和 9。 现在我想要的是得到所有总分的总和。在本例中为 50。

这是我尝试过的。

 public function GetsumScore($subject_id, $session_id, $section_id, $class_id) 
    {
        $this->db->select_sum('total');
        $this->db->where('subject_id', $subject_id);
        $this->db->where('session_id', $session_id);
        $this->db->where('section_id', $section_id);
        $this->db->where('class_id', $class_id);
        return $this->db->get('scores')->row(); 
    }

最后,如何在视图的表格中显示总和?

【问题讨论】:

  • 请提供有关您正在使用的框架/库的一些信息。由于您的问题的答案取决于框架/库的工作方式
  • 你的意思是 CodeIgniter 框架吗?
  • 请阅读:codeigniter.com/userguide3/database/…,请记住,您可以包含第二个参数来重命名结果字段,然后继续阅读Generating Query Results

标签: php sql codeigniter


【解决方案1】:

您可以通过这种方式直接将数据传递给视图

$data = array(
    'total_score' => $total_score // result came from GetsumScore method
);

$this->load->view('results_view', $data);

results_view.php

<html>
<?php 
//Access them like
echo "total score: " . $total_score; ?>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-30
    • 2022-10-04
    • 2019-11-13
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 2013-10-31
    相关资源
    最近更新 更多