Jump to content

Problem on where I want to display my calculation and other variables


u0867587

Recommended Posts

This code was given to me by mjdamato to help display a student and the course that student has take and within that the modules in the course and the sessions in course including module mark and grade and student's session marks and grades. I am very weak on php code as I have only done it once and that was following a php workbook 2 years ago. I am more of a database specialist. Below is the code given to me and you can also download the code from the attachment:

 

<?php

function outputModule($moduleID, $moduleName, $sessionData)    
  
  {        
  
  if(!count($sessionData)) { return false; }        
  
  $markTotal = 0;        
  $markGrade = 0;
  $weightSession = 0;
  $grade = "";
  $sessionsHTML = "";
  
  foreach($sessionData as $session)        {           
	  
	   $sessionsHTML .= "<p><strong>Session:</strong> {$session['SessionId']} <br><strong>Session Mark:</strong> {$session['Mark']}</strong> <br><strong>Session Weight Contribution</strong> {$session['SessionWeight']}%</p>\n";            
	   $markTotal += round($session['Mark'] / 100 * $session['SessionWeight']); 
	   $weightSession  += ($session['SessionWeight']);  
	   $markGrade = round($markTotal /  $weightSession * 100);
	   
if ($markGrade >= 70){
	$grade = "A";}      

else if ($markGrade >= 60 && $markGrade <= 69){
	$grade = "B";}

else if ($markGrade >= 50 && $markGrade <= 59){
	$grade = "C";}

else if ($markGrade >= 40 && $markGrade <= 49){
	$grade = "D";}

else if ($markGrade >= 30 && $markGrade <= 39){
	$grade = "E";}

else if ($markGrade >= 0 && $markGrade <= 29){
	$grade = "F";}
	   
	   }        
	   
	   $moduleHTML = "<p><br><strong>Module:</strong> {$moduleID} - {$moduleName} <br><strong>Module Mark:</strong> {$markTotal} <br><strong>Mark Percentage:</strong> {$markGrade} <br><strong>Grade:</strong> {$grade} </p>\n";       
	   
	    return $moduleHTML . $sessionsHTML;    }    
	    
	    $output = "";    
	    
	    $studentId = false;    
	    $courseId  = false;    
	    $moduleId  = false;    
	    
	    while ($row = mysql_fetch_array($result))    {               
		     
		     if($studentId != $row['StudentUsername'])        {            
			     
			     //Student has changed              
		     $studentId = $row['StudentUsername'];                    
		     
		     $output .= "<p><strong>Student:</strong> {$row['StudentForename']} {$row['StudentSurname']} ({$row['StudentUsername']})\n";          
		     
		      }        
		      
		     if($courseId != $row['CourseId'])        {         
			      
			      //Course has changed           
			       $courseId = $row['CourseId'];                    
			       
			       $output .= "<br><strong>Course:</strong> {$row['CourseId']} - {$row['CourseName']} <strong>Course Mark</strong> <strong>Grade</strong>  <br><strong>Year:</strong> {$row['Year']}</p>\n";           
			       
			        }      
			        
			 if($moduleId != $row['ModuleId'])        {            

				 //Module has changed            
				 if(isset($sessionsAry)) //Don't run function for first record           

				  {              
					  
					    //Get output for last module and sessions                
					    $output .= outputModule($moduleId, $moduleName, $sessionsAry);            }            
					    
					    //Reset sessions data array and Set values for new module           
					    
					     $sessionsAry = array();            
					     $moduleId    = $row['ModuleId'];         
					     $moduleName  = $row['ModuleName'];        }        
					     
					     //Add session data to array for current module      
					     
					     $sessionsAry[] = array('SessionId'=>$row['SessionId'], 'Mark'=>$row['Mark'], 'SessionWeight'=>$row['SessionWeight']);    
					     
					     }    //Get output for last module  
					     
					      $output .= outputModule($moduleId, $moduleName, $sessionsAry);   
					      
					       //Display the output    
					       echo $output;

      }
  }
    ?>

 

Below the what this code outputs:

 

 

Student: Mayur Patel (u0867587)

Course: INFO101 - Bsc Information Communication Technology      Course Mark

Year: 3

 

 

Module: CHI2550 - Modern Database Applications        Module Mark: 41          Mark Percentage: 68            Grade: B

 

Session: AAB            Session Mark: 72              Session Weight Contribution 20%

 

Session: AAE            Session Mark: 67              Session Weight Contribution 40%

 

 

Module: CHI2513 - Systems Strategy            Module Mark: 31          Mark Percentage: 62            Grade: B

 

Session: AAD              Session Mark: 61            Session Weight Contribution 50%

 

 

If you look above it shows that there is nothing in the Course Mark: section, what Course Mark is suppose to do is total up all the module mark percentages and then divide it by the number of modules. So the calculation will be adding up all the

$markGrade

and dividing it by count

$ModuleID

. In examle above it will be 68 (mark percentage for 1st module) + 62 (mark percentage for 2nd  module) / 2 (number of modules in the course). So the Course Mark should equal 65. 

 

The problem is that all of the calculations are in the foreach loop but where the course details and where the course marks are going to be placed are in the while loop. So the problem is that if the total Course Mark (lets say we call in $totalCourse) is displayed in the while loop it will provide an undifined index as the calculation will be placed with the other calculations in the foreach loop which is outside the while loop, if I do the calculation with in the while loop, it will only choose the 1st module mark percentage and divdie it by the first module only, this is obviously incorrect as it sohuld try and do this for all modules hence why my calculations are in the foreach loop which does this.

 

So how will change the code so that the course details can go from the while loop and be placed in the foreach loop so then I can include the calculation in the foreach loop.

 

Thank You and hopefully you understand it, just read it very carefully to understand it, it is very simple what I want to achieve but I do not know how to do it.

 

[attachment deleted by admin]

Link to comment
Share on other sites

underneith

 

$markGrade = 0;

 

insert

 

$markGradeSum = 0;
$totalLoops = 0;

 


 

underneith

 

$markGrade = round($markTotal /  $weightSession * 100);

 

insert

 

$markGradeSum += $markGrade;
$totalLoops++;

 


 

now output

 

echo $markGradeSum / $totalLoops;

 

if this is not EXACTLY what it is you need, I'm sure this gives you a very good understanding of what you need done..

 

:)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.