有没有一种方法可以仅使用PHP获得最高和最低分数? [英] Is there a way to get the highest and lowest scores using just PHP?

查看:70
本文介绍了有没有一种方法可以仅使用PHP获得最高和最低分数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个班上学生的成绩表。所有主题的所有测试和考试成绩都会整理并记录在那里。

I have a score table for students in a class. All tests and exam scores for subjects are collated and recorded there.

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;


总计列是根据控制器自动计算得出的。即所有考试成绩+考试成绩。
然后根据总分数获得最高和最低分数。

The Total column is automatically calculated from the controller. i.e all test scores + exam scores. Then the highest and lowest scores are gotten based on the total scores.

一切正常,但由于目前的情况(Covid-19 Pandemic),需要进行更改

Everything works fine but due to present circumstances (Covid-19 Pandemic), changes need to made and that is where the problem now surfaces.

现在,在学校关闭之前,仅输入测试成绩(即,从ca1到ca6)。他们无法编写考试,因此考试专栏为空白。

Now, before schools were shut down, only test scores were inputted (i.e from ca1 to ca6). They were not able to write their exams, hence a blank spot for the exam column.

为了纠正这一点,得出了一个结论来计算所有CA的总数,乘以40,再除以60。

To rectify that, a conclusion was made to calculate the total of all the CAs, multiply it by 40 and divide by 60.

CAs总数X 40/60

CAs Total X 40 /60

得分。

我不太擅长编码。可以说我比新手还差。但是,我尝试在视图中计算考试和总分。
因为现在是从查询中获得最高和最低的分数,所以现在我陷入困境

I'm not very knowledgeable in coding. One can say I'm worse than a novice. However, I tried to calculate the exam and Total scores in the view. I'm now stuck as to how to get the highest and lowest scores since they were gotten from a query

最高/最低模型得分


  public function GetHighestScore($subject_id, $session_id, $section_id, $class_id) 
    {
        $this->db->select_max('tot_score');
        $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_primary')->row(); 
    }

    public function GetLowestScore($subject_id, $session_id, $section_id, $class_id) 
    {
        $this->db->select_min('tot_score');
        $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_primary')->row(); 
    }

最高和最低得分的控制者

public function GetHighestScore($subject_id, $session_id, $section_id, $class_id)
    {
        $data = $this->primary_model->GetHighestScore($subject_id, $session_id, $section_id, $class_id); 
        return $data->tot_score;
    }


    public function GetLowestScore($subject_id, $session_id, $section_id, $class_id)
    {
        $data = $this->primary_model->GetLowestScore($subject_id, $session_id, $section_id, $class_id); 
        return $data->tot_score;
    }

视图

 <td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo $CI->GetHighestScore($value->subject_id, $value->session_id, $value->section_id, $value->class_id, $value->class_id); ?></td>

<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo $CI->GetLowestScore($value->subject_id, $value->session_id, $value->section_id, $value->class_id); ?></td>

自从我从视图计算考试和总分以来,它已经做到了代码为null,因为总分数现在是直接在视图中计算的。

Since I calculated the exam and total scores from the view, it has made the above codes null because the total score is now calculated straight in the view.

我现在有没有办法使用自己的计算方法获得全班最高和最低的成绩?还是我如何在控制器中进行此计算,以便像以前那样自动生成类中的总数,最高和最低?

Is there a way for me now to get the highest and lowest in class using the calculation I made? or how do I make this calculations in the controller so that the total, highest and lowest in class are automatically generated like they use to?

Contoller >

function assigngradeAction() 
{
      for($i=0; $i<count($this->input->post('number')); $i++)
                {

                    $data[]=array(
                        'section_id' => $this->input->post('section_id'),
                        'subject_id' => $this->input->post('subject_id'),
                        'class_id' => $this->input->post('class_id')[$i],
                        'student_id' => $this->input->post('student_id')[$i],
                        'session_id' => $this->input->post('session_id'),
                        'ca1' => $this->input->post('ca1')[$i],
                        'ca2' => $this->input->post('ca2')[$i],
                        'ca3' => $this->input->post('ca3')[$i],
                        'ca4' => $this->input->post('ca4')[$i],
                        'project' => $this->input->post('project')[$i],
                        'affective' => $this->input->post('affective')[$i],
                        'psychomotor' => $this->input->post('psychomotor')[$i],
                        'exam' => $this->input->post('exam')[$i],
            'tot_score'=> $this->input->post('ca1')[$i] + $this->input->post('ca2')[$i] + $this->input->post('ca3')[$i] + $this->input->post('ca4')[$i] + $this->input->post('project')[$i] + $this->input->post('affective')[$i] + $this->input->post('psychomotor')[$i] + $this->input->post('exam')[$i],

        }






        //var_dump($data);

        $inserted = $this->primary_model->add2($data2);
        if($inserted)
        {
            $this->session->set_flashdata('msg', '<div class="alert alert-success">Grade Added successfully</div>');
            //Echo back success json
            redirect('admin/teacher/GetStudentForSubject');
        }

    }

查看

 <tr>
<td style="border: 1px solid black; font-size:11px;width:120px;white-space: nowrap;height:30px;">
<?php echo $CI->GetSubjectNameWithID($value->subject_id); ?>
</td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo $value->ca1; ?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo $value->ca3; ?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo $value->ca4; ?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo $value->project; ?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo $value->affective; ?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo $value->psychomotor; ?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo $value->ca1 + $value->ca3 + $value->ca4 + $value->project + $value->affective + $value->psychomotor; ?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo round($value->ca1 *40/60 + $value->ca3*40/60 + $value->ca4*40/60 + $value->project*40/60 + $value->affective*40/60 + $value->psychomotor*40/60, 1); ?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo round($value->tot_score = $value->ca1 *40/60 + $value->ca3*40/60 + $value->ca4*40/60 + $value->project*40/60 + $value->affective*40/60 + $value->psychomotor*40/60 + $value->ca1 + $value->ca3 + $value->ca4 + $value->project + $value->affective + $value->psychomotor, 1); ?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo $grade; ?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo $CI->GetHighestScore($value->subject_id, $value->session_id, $value->section_id, $value->class_id, $value->class_id); ?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo $CI->GetLowestScore($value->subject_id, $value->session_id, $value->section_id, $value->class_id); ?></td>
<td style="border: 1px solid black; font-size:11px;width:120px;white-space: nowrap;">
                                                <?php

$scores2 = $CI->GetSubjectScores($value->subject_id, $value->session_id, $value->section_id, $value->class_id); //echo $value->pos;

$scores = array_column($scores2, 'tot_score');

$pos = array_search($value->tot_score, $scores);
 //var_dump($pos);
 $number = $pos + 1;

echo $CI->ordinal($number);
?>
</td>
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo $remark; ?></td>
</tr>


推荐答案

在您存储控制器的位置考试编号您需要检查一个条件,即考试编号是否为空,然后计算总ca(s),然后乘除(应用您的逻辑)。

In your controller, where you store exam no. you need to check a condition i.e. if the exam no. is empty then calculate the total ca(s) and then multiply and divide(apply your logic).


请记住,这只是一个临时解决方案,一旦所有数据都保存了
,则要撤消更改或将其注释掉,以便以后
使用它。




控制器->函数assigngradeSingleStudentAction()-> elseif条件 //(我假设此问题仅适用于elseif条件,如您的代码所示)

else if($this->input->post('class') >= 4 && $this->input->post('class') <= 17)
{

    $data2['section_id']  = $this->input->post('section_id');
    $data2['subject_id']  = $this->input->post('subject_id');
    $data2['class_id']    = $this->input->post('class_id');
    $data2['student_id']  = $this->input->post('student_id');
    $data2['session_id']  = $this->input->post('session_id');
    $data2['ca1']         = $this->input->post('ca1');
    $data2['ca2']         = $this->input->post('ca2');
    $data2['ca3']         = $this->input->post('ca3');
    $data2['ca4']         = $this->input->post('ca4');
    $data2['project']     = $this->input->post('project');
    $data2['affective']   = $this->input->post('affective');
    $data2['psychomotor'] = $this->input->post('psychomotor');

    /* No change till here */
    if( !empty( $this->input->post('exam') )){

        $data2['exam']    = $this->input->post('exam');

    }else{

        $data2['exam']    = ( ( $data2['ca1'] + $data2['ca2'] + $data2['ca3'] + $data2['ca4']) * 40 )/ 60;
    }  // comment this else condition after your work is done.

    $data2['tot_score']   = $this->input->post('ca1') + $this->input->post('ca2') + $this->input->post('ca3') + $this->input->post('ca4') + $this->input->post('project') + $this->input->post('affective') + $this->input->post('psychomotor') + $data2['exam'];
}

这应该对您有帮助。

这篇关于有没有一种方法可以仅使用PHP获得最高和最低分数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆