Jump to content

php code help (mjdamto may know this as he gave me this code previously)


u0867587
 Share

Recommended Posts

Hi,

 

Look at this code below:

 

<?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']} <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); 
             
        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']} </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; 

}
      }
  }
    ?>

 

This code allallows me to make calculations and display a student's course and linked with it the course the modules in the course and linked with modules are all the sessions. It is able to display what marks each student have got for each module and session.

 

Now look at code below, it is able to display modules and in those modules the sessions that link to those modules:

 

 

<?php 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']); 
         
}

What I want to know is how can I do something similar for course so that it picks out the right modules depending on the course it displays. There maybe some code that needs to be added in the function.

Link to comment
Share on other sites

I am sorry for this but I am a database specialist, I have to do php because there is no one else. I have only done php once and that was two years ago following a bacic workbook. But I do appreciate the help you have given me. To be very honest this is the very last thing I need to do and then this document is done and the document is finished.

 

What I wanted to do is add all of the module marks ($markGrade) and then divide it by the number of modules ($totalLoops) so that I get the Student's course mark. Because I need to use the calculation in foreach loop, I have to move the course details from the while loop and put it in the for each loop so I tried to do it as the same as how you did the module details where you managed to get all sessions per module. I wanted to get all modules per course. It works in the while loop when you gave me the code but because I need to move the course details into the foreach loop, I tried to do this below but it ends up coming up with notice saying undefined variable $SessionData (I know its a brace in wrong place) but even if I sort this out it will not display anything.

 

Below is what I tried but it didn't work:

 

<?php
     
     if($num ==0){
    echo "<p>Sorry, No Records were found from this Search</p>";}
    else{
    
  function outputModule($courseID, $courseName, $moduleID, $moduleName, $sessionData)    
  
  {        
  
  if(!count($sessionData)) { return false; }        
  
  $markTotal = 0;        
  $markGrade = 0;
  $markGradeSum = 0;
  $totalLoops = 0;
  $weightSession = 0;
  $courseTotal = 0;
  $grade = "";
  $sessionsHTML = "";
  $courseHTML = "";
  
  foreach($sessionData as $session)        {           
	  
	   $courseHTML = "<p><br><strong>Course:</strong> {$courseID} - {$courseName} Mark: {$courseTotal}</p>/n";       
	  
	   $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);
	   $totalLoops++;
	   $markGradeSum += $markGrade;
	   $courseTotal = ($markGradeSum / $totalLoops);
	   
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 $sessionHTML . $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'])        {            

          
				 if(isset($modulessAry))           

				  {              
					  
					                 
					    $output .= outputModule($courseId, $courseName, $modulesAry);            }            
					    
					               
					    
					     $modulesAry = array();            
					     $courseId    = $row['CourseId'];         
					     $courseName  = $row['CourseName'];        }        
					     
					           
					     
					     $modulesAry[] = array('CourseId'=>$row['CourseId'], 'CourseName'=>$row['CourseName']);    
					     
					     						     						 					          
			        
			 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 is what I want for the result output:

 

Student Username: 'u0867587'

Student: Mayur Patel (u0867587)

Course: INFO101 - Bsc Information Communication Technology      Course Mark 65

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%

 

 

Thank you and sorry for all the answers I have asked from you and others.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

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.

 Share

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