MaryUghojor
MaryUghojor

Reputation: 87

Is there a way to get the highest and lowest scores using just 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;


A student's score sheet

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.

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

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.

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

CAs Total X 40 /60

That would give the exam scores.

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

The model for highest/lowest score


  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(); 
    }

The controller for highest and lowest score

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

The view

 <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>

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

    }

View

 <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>

Upvotes: 0

Views: 229

Answers (2)

MaryUghojor
MaryUghojor

Reputation: 87

This is what I did using @sauhardnc solution

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'=> !empty( $this->input->post('exam')[$i] ) ? $this->input->post('exam')[$i] : ($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]) * 40 / 60,
                        '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] + ($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]) * 40 / 60,
}   

                    );

                }




                 $inserted = $this->primary_model->add1($data);
                 if($inserted > 0)
                 {
                     $this->session->set_flashdata('msg', '<div class="alert alert-success">Grade Added successfully</div>');
                     //Echo back success json
                     redirect('admin/primary/index');
                  }           

    }

Upvotes: 0

sauhardnc
sauhardnc

Reputation: 1961

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).

Remember this is only a temporary solution, once all the data is saved, you should undo the changes or comment it out if you want to use it later.



Controller -> function assigngradeSingleStudentAction() -> elseif condition //(I'm assuming this issue is only for elseif condition as your code suggests)

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

This should help you.

Upvotes: 1

Related Questions