LOVE KUMAR
LOVE KUMAR

Reputation: 35

How to display all added subject marks or total marks of student in array

I am getting all student and their subject marks in foreach loop and at last I am adding in loop.

What I want is all total marks of student in an array. 1. I am getting the all the subjects marks according subject id. 2. Fetching the student data from database and their marks using foreach loop. 3. Adding the marks and getting total marks of all subjects. 4. Return an array of all total marks .

This is what I tried :

foreach ($result_detail as $key => $value) {
    $total_marks += $value['get_marks'];
    $total_rank = $value['total_rank'];

  for ($i=0; $i < count($value) ; $i++) { 
            $c = array();
      $grades = array();
       $c[$i] = $total_marks;
    }

}

Upvotes: 1

Views: 1541

Answers (1)

MorganFreeFarm
MorganFreeFarm

Reputation: 3733

Then .. you should set variable as array and append values to it:

<?php

$total_marks = []; // Set default values to make sure it won't throw error for undefined variable below in code
$total_rank = 0; 

 foreach ($result_detail as $key => $value) {
   $total_marks[] = $value['get_marks']; // append values to array
   $total_rank += $value['total_rank']; // add rank to current rank
}

echo '<pre>';
var_dump($total_marks);
echo $total_rank;

Notice that I also changed $total_rank now I add rank each iteration instead of overwriting the value.

Upvotes: 3

Related Questions