Welcome Guest, Not a member yet? Register   Sign In
What is wrong with this code pls
#1

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

}
Reply
#2

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

@soapz,

You might want to try using the CI profiler ( https://www.codeigniter.com/user_guide/g...iling.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?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB