Jump to content

How to retrieve this variable


u0867587

Recommended Posts

Hi,

 

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:

 

 <?php
     
     if($num ==0){
    echo "<p>Sorry, No Records were found from this Search</p>";}
    else{
    
  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
https://forums.phpfreaks.com/topic/250521-how-to-retrieve-this-variable/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.