CodeIgniter Forums
What is wrong with this code pls - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: What is wrong with this code pls (/showthread.php?tid=74171)



What is wrong with this code pls - soapz - 08-10-2019

Dear Team,
Please this code can run on view but returns zero or null for total_marks and Average score whenever i run it on controller.
Tell me what i am doing wrong. Thanks 

function testme ($exam_id,$class_id){

$soap_data['exam_id'] = $exam_id;
$soap_data['class_id'] = $class_id;

        $soap_data['year']       = $this->db->get_where('settings' , array('type'=>'running_year'))->row()->description;
        if($soap_data['class_id'] != '' && $soap_data['exam_id'] != ''){


$subjects = $this->db->get_where('subject' , array('class_id' => $class_id , 'year' => $running_year))->result_array();
foreach($subjects as $row){ echo $row['name'];}



$students = $this->db->get_where('enroll' , array('class_id' => $class_id , 'year' => $running_year))->result_array();
    foreach($students as $row){

$total_marks = 0;
            $total_grade_point = 0;
                    foreach($subjects as $row2)
$obtained_mark_query = $this->db->get_where('mark' , array(
'class_id' => $class_id , 
'exam_id' => $exam_id , 
'subject_id' => $row2['subject_id'] , 
'student_id' => $row['student_id'],
'year' => $running_year
));
if ( $obtained_mark_query->num_rows() > 0) {
$obtained_marks = $obtained_mark_query->row()->mark_obtained;
if ($obtained_marks >= 0 && $obtained_marks != '') {
$grade = $this->crud_model->get_grade($obtained_marks);
$total_grade_point += $grade['grade_point'];
}
$total_marks += $obtained_marks;

}

$soap_data['Total_score'] = $total_marks;
$this->db->where('class_id' , $class_id);
$this->db->where('year' , $running_year);
$this->db->from('subject');
$number_of_subjects = $this->db->count_all_results();
$average_score= ($soap_data['Total_score'] / $number_of_subjects);
$avg =  round($average_score,2);
$soap_data['Average_score'] = $avg;
}



        $query = $this->db->get_where('total_mark' , array(
                    'exam_id' => $soap_data['exam_id'],
                        'class_id' => $soap_data['class_id'],
                            'Total_score' => $soap_data['Total_score'],
                                'Average_score' => $soap_data['Average_score'],
                                    'year' => $soap_data['year']
                ));
        if($query->num_rows() < 1) {
           $students = $this->db->get_where('enroll' , array('class_id' => $soap_data['class_id'] , 'year' => $soap_data['year']))->result_array();
            foreach($students as $row) {
               $soap_data['student_id'] = $row['student_id'];
                $this->db->insert('total_mark' , $soap_data);
            }
}
}

}


RE: What is wrong with this code pls - InsiteFX - 08-11-2019

Please place your code into code tags so that we can read it better.

[ code] your code [ /code]

remove the space in the code tags.


RE: What is wrong with this code pls - php_rocs - 08-12-2019

@soapz,

You might want to try using the CI profiler ( https://www.codeigniter.com/user_guide/general/profiling.html ). This will allow you to see the executed query (as well as other useful details) which may give you insight into what is wrong with your query.

What is your process for debugging code?