Codeigniter在列值上使用Select_SUM构建Foreach查询?

时间:2017-03-09 10:32:54

标签: arrays codeigniter foreach sum

我成功地回应了第一个CA","第二个CA"和"考试"在Codeigniter视图上。您将在下面的图片中找到

报告视图和代码
View report screenshot missing foreach query for Total Column

以下是

下的查看代码
  <td bgcolor="#CCCCCC"><div align="center" class="style2"><?php
                    $obtained_second_ca_query = $this->db->get_where('mark' , array(
                                                'subject_id' => $row3['subject_id'],
                                                    'exam_id' => $exam_id,
                                                        'class_id' => $class_id,
                                                            'student_id' => $student_id , 
                                                                'year' => $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description
                                            ));
                    if($obtained_second_ca_query->num_rows() > 0){
                        $marks2 = $obtained_second_ca_query->result_array();
                        foreach ($marks2 as $row4) {
                            echo $row4['second_ca'];
                            $total_marks += $row4['second_ca'];
                        }
                    }
                ?></div></td>
<td bgcolor="#CCCCCC"><div align="center" class="style2"></div></td>
<td bgcolor="#CCCCCC"><div align="center" class="style2"></div></td>
<td bgcolor="#CCCCCC"><div align="center" class="style2"></div></td>
<td bgcolor="#CCCCCC"><div align="center" class="style2"><?php
                    $obtained_exam_score_query = $this->db->get_where('mark' , array(
                                                'subject_id' => $row3['subject_id'],
                                                    'exam_id' => $exam_id,
                                                        'class_id' => $class_id,
                                                            'student_id' => $student_id , 
                                                                'year' => $this->db->get_where('settings' , array('type' => 'running_year'))->row()->description
                                            ));
                    if($obtained_exam_score_query->num_rows() > 0){
                        $marks3 = $obtained_exam_score_query->result_array();
                        foreach ($marks3 as $row4) {
                            echo $row4['exam_score'];
                            $total_marks += $row4['exam_score'];
                        }
                    }
                ?></div></td>
<td bgcolor="#CCCCCC"><div align="center" class="style2"><strong></strong></div></td>

但是,我需要在总列中求和它们的值。请帮忙。提前谢谢。

这是我的数据库表(&#39;标记&#39;)截图
View my database table screenshot

还可以在下面找到我的模型代码。

function get_obtained_marks( $exam_id , $class_id , $subject_id , $student_id) {
    $marks = $this->db->get_where('mark' , array(
                                'subject_id' => $subject_id,
                                    'exam_id' => $exam_id,
                                        'class_id' => $class_id,
                                            'student_id' => $student_id))->result_array();

    foreach ($marks as $row) {
        echo $row['mark_obtained'];
    }
}

function get_obtained_second_ca( $exam_id , $class_id , $subject_id , $student_id) {
    $marks2 = $this->db->get_where('mark' , array(
                                'subject_id' => $subject_id,
                                    'exam_id' => $exam_id,
                                        'class_id' => $class_id,
                                            'student_id' => $student_id))->result_array();

    foreach ($marks2 as $row) {
        echo $row['second_ca'];
    }
}

function get_obtained_exam_score( $exam_id , $class_id , $subject_id , $student_id) {
    $marks3 = $this->db->get_where('mark' , array(
                                'subject_id' => $subject_id,
                                    'exam_id' => $exam_id,
                                        'class_id' => $class_id,
                                            'student_id' => $student_id))->result_array();

    foreach ($marks3 as $row) {
        echo $row['exam_score'];
    }

请帮助您解决此问题。我需要一个foreach查询来总结&#39; mark_obtained&#39; +&#39; second_ca&#39; +&#39; exam_score&#39;在我的总栏目中。

1 个答案:

答案 0 :(得分:0)

  

在每个查询中添加select子句。

$marks3 = $this->db->select('*,(mark_obtained + second_ca + exam_score) as total')->get_where('mark', array('subject_id' =>$subject_id, 'exam_id' => $exam_id,'class_id' => $class_id,'student_id' => $student_id))->result_array();