Jump to content

How to retrieve this variable


Recommended Posts



I am not very good with php but this code was kindly given to me by mdjamato, except I added a variable $markGradeSum to add all the $markGrade. Problem is that I will get notice saying undefined variable $markGradeSum because the $markGradeSum is in the foreach loop which it needs to be in for the right calculation but where I want to output the variable is in the while loop in this line:


 $output .= "<br><strong>Course:</strong> {$row['CourseId']} - {$row['CourseName']} <strong>Course Mark</strong> <strong>Grade</strong>  <br><strong>Year:</strong> {$row['Year']  $markGradeSum}</p>\n"; 


Below is whole code:


     if($num ==0){
    echo "<p>Sorry, No Records were found from this Search</p>";}
  function outputModule($moduleID, $moduleName, $sessionData)    
  if(!count($sessionData)) { return false; }        
  $markTotal = 0;        
  $markGrade = 0;
  $weightSession = 0;
  $markGradeSum = 0;
  $grade = "";
  $sessionsHTML = "";
  $courseHTML = "";
  foreach($sessionData as $session)        {           
	   $sessionsHTML .= "<p><strong>Session:</strong> {$session['SessionId']} <strong>Session Mark:</strong> {$session['Mark']}</strong> <strong>Session Weight Contribution</strong> {$session['SessionWeight']}%</p>\n";            
	   $markTotal += round($session['Mark'] / 100 * $session['SessionWeight']); 
	   $weightSession  += ($session['SessionWeight']);  
	   $markGrade = round($markTotal /  $weightSession * 100);
	   $markGradeSum = round($markGrade);
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} <strong>Module Mark:</strong> {$markTotal} <strong>Mark Percentage:</strong> {$markGrade} <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']  $markGradeSum}</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;




How can I retrieve the variable so it doesn't come up with the notice but be able to display $markGradeSum to the places I want them in (one in foreach loop for calculation and other in while loop for output).


Thank You

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.

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.