Jump to content

Calculation displays correct answer in foreach loop but not in while loop. Why?


u0867587
 Share

Recommended Posts

Below is my output on the browser:

 

    Student: Kevin Smith (u0867587)

    Course: INFO101 - Bsc Information Communication Technology Course Mark 70 Grade

    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%

 

Now where it says course mark above it says 70. This is incorrect as it should be 65 (The average between the module marks percentage should be 65 in the example above) but for some stange reason I can get the answer 65. I have a variable called $courseMark and that does the calculation. Now if the $courseMark is echo outside the where loop, then it will equal 65 but if it is put in while loop where I want the variable to be displayed, then it adds up to 70. Why does it do this.

   

    Below is the code:

 

    $sessionMark = 0;
    $sessionWeight = 0;
    $courseMark = 0;
    
    $output = ""; 
    
    $studentId = false; 
    $courseId  = false; 
    $moduleId  = false; 
    
    while ($row = mysql_fetch_array($result)) 
    { 
    	
    	
    	$sessionMark += round($row['Mark'] / 100 * $row['SessionWeight']);
    	$sessionWeight  += ($row['SessionWeight']); 
        $courseMark = ($sessionMark /  $sessionWeight * 100);
    
          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>" round($courseMark) "<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; 

 

I think the problem is that it is outputting the answer of the calculation only for the first session mark. How in the while loop can I do it so it doesn't display it for the first mark only but for all the session marks so that it ends up showing the correct answer 65 and not 72?

Link to comment
Share on other sites

Sorry this is a slight dulicate of a previous question I asked yesterday because I think I messed up when trying to ask this question and I think I made iu unclear for everbody

 

Below is my output on the browser:

 

    Student: Kevin Smith (u0867587)

    Course: INFO101 - Bsc Information Communication Technology Course Mark 72 Grade

    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%

 

    65

 

Now where it says course mark above it says 72. This is incorrect as it should be 65 (The average between the module marks percentage should be 65 in the example above) but for some stange reason I can get the answer 65 (as it is displayed below the example). I have a variable called $courseMark and that does the calculation. Now if the $courseMark is echo outside the where loop, then it will equal 65 but if it is put in while loop where I want the variable to be displayed, then it adds up to 72. Why does it do this and how can I get the answer 65 in the while loop.

   

    Below is the relevant code (I havn't include all code as this could confuse you so I included code that is relevant for this question):

    $sessionMark = 0;
    $sessionWeight = 0;
    $courseMark = 0;
    
    $output = ""; 
    
    $studentId = false; 
    $courseId  = false; 
    $moduleId  = false; 
    
    while ($row = mysql_fetch_array($result)) 
    { 
    	
    	
    	$sessionMark += ($row['Mark'] / 100 * $row['SessionWeight']);
    	$sessionWeight  += ($row['SessionWeight']); 
        $courseMark = round($sessionMark /  $sessionWeight * 100);
    
             
        if($courseId != $row['CourseId']) 
        { 
    
            //Course has changed 
            $courseId = $row['CourseId']; 
    
            $output .= "<br><strong>Course:</strong> {$row['CourseId']} - {$row['CourseName']} <strong>Course Mark</strong> $courseMark <strong>Grade</strong>  <br><strong>Year:</strong> {$row['Year']}</p>\n"; 
    
        } 
    
    
    
    //Display the output 
    echo $output; 
echo $courseMark;

 

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.