Codeigniter:获取和求和字段值

时间:2017-03-13 17:45:14

标签: php mysql arrays codeigniter foreach

请帮忙...

我想写一个 查询来显示每个学生的所有分数的总和 (或者每个表的记录 - 第一个CA +第2个CA + EXAM SCORE = TOTAL )。

我可以成功显示标记,但无法填写“总计”。

请查看下面的图片。

View table

请在下面找到我的“标记表”和“主题表”结构...

View table structure

使用MVC架构......

这是我的模型

function get_obtained_first_ca( $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['first_ca'];
    }}

         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'];
    }
}

这是我的autoload.php文件内容

$autoload['model'] = array('crud_model'); // The crud_model is my main model file.

这是我的控制器

function __construct()
{
    parent::__construct();
    $this->load->database();
    $this->load->library('session');

以下是我的观点

      <table>
        <tbody>
   <thead>
    <tr>
        <td style="text-align: center;">Subject</td>
        <td style="text-align: center;">1st CA</td>
        <td style="text-align: center;">2nd CA</td>
         ...
        <td style="text-align: center;">Total</td>
    </tr>
</thead>
    <?php 
        $total_marks = 0;
        $total_grade_point = 0;
        $subjects = $this->db->get_where('subject' , array(
                'class_id' => $class_id , 'year' => $running_year
        ))->result_array();
        foreach ($subjects as $row3):
    ?>
        <tr>
            <td style="text-align: center;"><?php echo $row3['name'];?></td>
            <td style="text-align: center;">
                <?php
                    $obtained_mark_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_mark_query->num_rows() > 0){
                        $marks = $obtained_mark_query->result_array();
                        foreach ($marks as $row4) {
                            echo $row4['first_ca'];
                            $total_marks += $row4['first_ca'];
                        }
                    }
                ?>
            </td>
            <td style="text-align: center;"><?php
                    $obtained_mark_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_mark_query->num_rows() > 0){
                        $marks = $obtained_mark_query->result_array();
                        foreach ($marks as $row4) {
                            echo $row4['second_ca'];
                            $total_marks += $row4['second_ca'];
                        }
                    }
                ?></td>
            </td>
            <td style="text-align: center;">
            <?php
                echo $total_mark; // Here is where I need to fetch my Total

                ?>
        </tr>
    <?php endforeach;?>

我们非常感谢您帮助我填写整栏的努力。提前谢谢!

0 个答案:

没有答案