Want to display course details at top not bottom
<p>I managed to sort out the calculation but there is just one last question and then this page is completley finished. The only problem is that to be able to get the answer of the calculation, I had to display the course details at the bottom, so the output looks like this below:</p>

<pre><code>Student: Mayur Patel (u0867587) 
Module: CHI2550 - Modern Database Applications  Module Mark:    41  Mark Percentage:    68  Grade:  B

Session Session Mark    Session Weight
AAB 72  20%
Session Session Mark    Session Weight
AAE 67  40%

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

Session Session Mark    Session Weight
AAD 61  50%
</code></pre>

<p>Course: INFO101 - Bsc Information Communication Technology  Course Mark: 65</p>

<p>I want the course details to be outputted above the modules so it looks like this below:</p>

<pre><code>Student: Mayur Patel (u0867587) 
Course: INFO101 - Bsc Information Communication Technology  Course Mark: 65

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

Session Session Mark    Session Weight
AAB 72  20%
Session Session Mark    Session Weight
AAE 67  40%

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

Session Session Mark    Session Weight
AAD 61  50%
</code></pre>

<p>Problem is that if I keep the course details at the bottom then the calculation works. I move it to the top and then the calculation would not work as the calculation for the $courseGrade is at the bottom and so for the Course Mark it will display 0. </p>

<p>So my question is how can I move the course details to the top and still have the answer of the calculation displayed for the course mark?</p>

<pre><code>Below is the code:


$dataArray = array(); 

        while ($row = mysql_fetch_array($result)) { 
            $dataArray[$row['CourseId']]['CourseName'] = $row['CourseName']; 
            $dataArray[$row['CourseId']]['Modules'][$row['ModuleId']]['ModuleName'] = $row['ModuleName']; 
            $dataArray[$row['CourseId']]['Modules'][$row['ModuleId']]['Sessions'][$row['SessionId']]['Mark'] = $row['Mark']; 
            $dataArray[$row['CourseId']]['Modules'][$row['ModuleId']]['Sessions'][$row['SessionId']]['SessionWeight'] = $row['SessionWeight']; 

                 if($studentId != $row['StudentUsername']) 
    { 

        //Student has changed 
        $studentId = $row['StudentUsername']; 

        $output .= "&lt;strong&gt;Student:&lt;/strong&gt; {$row['StudentForename']} {$row['StudentSurname']} ({$row['StudentUsername']})\n"; 

    } 
        } 

        // just for debugging purposes, let's do a print_r of the array 
        // eliminate this line when you don't need it anymore 
        // print_r($dataArray); 

        foreach ($dataArray as $courseId =&gt; $courseData) {  
           // elaborate course data 

           // elaborate course data 
           $moduleCount = 0;
           $courseTotal = 0;
           $courseGrade = 0;

           $courseHTML = ""; 

           $courseHTML .= "&lt;br&gt;&lt;table&gt;&lt;tr&gt;&lt;th&gt;Course:&lt;/th&gt;&lt;td&gt;" . $courseId .  " - "  . $courseData['CourseName'] . "&lt;/td&gt;"; 

           foreach ($courseData['Modules'] as $moduleId =&gt; $moduleData) { 
             // elaborate module data 
             $moduleHTML = "";

             $moduleHTML .= "&lt;br&gt;&lt;table&gt;&lt;tr&gt;&lt;th&gt;Module:&lt;/th&gt;&lt;td&gt;" . $moduleId . " - " . $moduleData['ModuleName'] ."&lt;/td&gt;"; 

             $markTotal = 0;  
             $markGrade = 0;  
             $weightSession = 0; 
             $grade = "";  
             $sessionsHTML = ""; 


             foreach ($moduleData['Sessions'] as $sessionId =&gt; $sessionData) { 
                // elaborate session data 
                $markTotal += round($sessionData['Mark'] / 100 * $sessionData['SessionWeight']);  
                $weightSession  += ($sessionData['SessionWeight']);  
                $sessionsHTML .= "&lt;table&gt;&lt;tr&gt;&lt;th&gt;Session&lt;/th&gt;&lt;th&gt;Session Mark&lt;/th&gt;&lt;th&gt;Session Weight&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;" . $sessionId . "&lt;/td&gt;&lt;td&gt;" . $sessionData['Mark'] . "&lt;/td&gt;&lt;td&gt;" . $sessionData['SessionWeight'] ."%&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;\n";  
             } 
             $markGrade = round($markTotal /  $weightSession * 100); 

              // To count the modules, simply add 1 to the counter
             $moduleCount++;
             // Add the mark grade to the course total
             $courseTotal += $markGrade;


             if ($markGrade &gt;= 70) { $grade = "A"; }  
             else if ($markGrade &gt;= 60 &amp;&amp; $markGrade &lt;= 69) { $grade = "B"; }  
             else if ($markGrade &gt;= 50 &amp;&amp; $markGrade &lt;= 59) { $grade = "C"; }  
             else if ($markGrade &gt;= 40 &amp;&amp; $markGrade &lt;= 49) { $grade = "D"; }  
             else if ($markGrade &gt;= 30 &amp;&amp; $markGrade &lt;= 39) { $grade = "E"; }  
             else if ($markGrade &gt;= 0 &amp;&amp; $markGrade &lt;= 29) { $grade = "F"; }              

             $moduleHTML .= " &lt;th&gt;Module Mark:&lt;/th&gt;&lt;td&gt;" . $markTotal . "&lt;/td&gt;&lt;th&gt;Mark Percentage:&lt;/th&gt;&lt;td&gt;" . $markGrade . "&lt;/td&gt;&lt;th&gt;Grade:&lt;/th&gt;&lt;td&gt;" . $grade . " &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br&gt;";   
             $output .= $moduleHTML . $sessionsHTML; 
           }  // &lt;-- end of sessions foreach 
         }  // &lt;-- end of modules foreach 

         // at the end of each course, you can calculate the course grade
           $courseGrade = ($courseTotal / $moduleCount);

         $courseHTML .= " &lt;th&gt;Course Mark:" . $courseGrade . "&lt;/th&gt;&lt;/tr&gt;&lt;/table&gt;";

           $output .= $courseHTML;
          //Display the output  
       echo $output;  

       }  // &lt;-- end of courses foreach
</code></pre>
