I have a slight calculation probelm.
Below is the my output:
Student: Mayur Patel (u0867587)
Course: INFO101 - Bsc Information Communication Technology Course Mark:62
Module: CHI2550 - Modern Database Applications Module Mark: 41 Mark Percentage: 68 Grade: B
Session Session Mark Session Weight
AAB 72 20%
Session Session Mark Session Weight
AAE 67 40%
Module: CHI2513 - Systems Strategy Module Mark: 31 Mark Percentage: 62 Grade: B
Session Session Mark Session Weight
AAD 61 50%
There is only one problem with this output. The only last little probelm is that the Course Mark is incorrect. It should add up to 65 (adding the mark percentage together (68 + 63 = 130) and divide by number of modules (2) and that should equal 65). Instead it adds up to 62. Now the calculations I have done is 200% correct. The problem those is that the the reason it is outputting 62 is because it is only selecting the rows for the last module (so calculation it is doing is 62 divide by 1 which equals 62). Now the reason for this is because my foreach loop where the course details and calculation is in only outputs one course detail from the database (as there is no need to show the course details multiple times). Because of this the calculation is only picking out rows from the last row only.
What I want to know is that is there a way the calculation can go through all the rows but I can keep it in the same foreach loop which displays only once course detail. If calculation is not in same foreach loop then I will get undefined variables.
Thank You
Below is the code I believe is relevant for this question. The foreach is for the course details only.
$output = "";
$studentId = false;
// load query data in a multidimensional array
$dataArray = array();
while ($row = mysql_fetch_array($result)) {
$dataArray[$row['CourseId']]['CourseName'] = $row['CourseName'];
$dataArray[$row['CourseId']]['ModuleId'] = $row['ModuleId'];
$dataArray[$row['CourseId']]['SessionWeight'] = $row['SessionWeight'];
$dataArray[$row['CourseId']]['Mark'] = $row['Mark'];
$dataArray[$row['CourseId']]['Modules'][$row['ModuleId']]['ModuleName'] = $row['ModuleName'];
$dataArray[$row['CourseId']]['Modules'][$row['ModuleId']]['Sessions'][$row['SessionId']]['Mark'] = $row['Mark'];
$dataArray[$row['CourseId']]['Modules'][$row['ModuleId']]['Sessions'][$row['SessionId']]['SessionWeight'] = $row['SessionWeight'];
if($studentId != $row['StudentUsername'])
{
//Student has changed
$studentId = $row['StudentUsername'];
$output .= "<strong>Student:</strong> {$row['StudentForename']} {$row['StudentSurname']} ({$row['StudentUsername']})\
";
}
}
// just for debugging purposes, let's do a print_r of the array
// eliminate this line when you don't need it anymore
// print_r($dataArray);
foreach ($dataArray as $courseId => $courseData) {
// elaborate course data
$markTotal = 0;
$markGrade = 0;
$weightSession = 0;
$moduleCount = 0;
$courseTotal = 0;
$markTotal += round($courseData['Mark'] / 100 * $courseData['SessionWeight']);
$weightSession += ($courseData['SessionWeight']);
$markGrade += round($markTotal / $weightSession * 100);
$moduleCount += count($courseData['ModuleId']);
$courseTotal += ($markGrade / $moduleCount);
echo $output;
} // <-- end of courses foreach