I am doing a project and our group are nearly finsihed. I just need one last thing to sort out and we are done. Below is our current output on the browser:

Course: INFO101 - Bsc Information Communication Technology Course Mark:



Course: INFO101 - Bsc Information Communication Technology Course Mark:


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%

As you can see above it shows the course details twice, this should not happen as it should only show it once but I do not know how to do this. The course details are dislayed by this code in the function :
Code:
`echo "<p><br><strong>Course:</strong> {$courseId} - {$courseName} <strong>Course Mark:</strong></p><br>\n";`
I posted this question on another website and this is what one person said what is happening and what I should do:

"This code does not cause the replication you are seeing. The output you've specified only prints that out once. So its down to usage.

This means you are iterating a block outside of this function and are calling this function twice for the desired module. That indicates that these modules: CHI2550 and CHI2513 both belong to course INFO101. Since these are directly printed and not captured, this indicates that you have called the function within a loop, but are not printing it until after the loop. The replication appears this way since they are echo'd out directly.

Easiest fix is to handle the data returned as an array. If you provide a referenced array to write into, you can easily append to the index and implode them after to create the strings."

He is correct here but the problem is that I do not know how to implement this on the code, can somebody show an example on how this can be done on the code I have written below:

Code:
    <?php
          if (isset($_POST['submit'])) {
    
          $query = "
              SELECT st.CourseId, c.CourseName, st.Year, st.StudentUsername, st.StudentForename, st.StudentSurname,
              s.ModuleId, m.ModuleName, m.Credits, s.SessionId, s.SessionWeight, gr.Mark, gr.Grade
              FROM Course c
              INNER JOIN Student st ON c.CourseId = st.CourseId
              JOIN Grade_Report gr ON st.StudentId = gr.StudentId
              JOIN Session s ON gr.SessionId = s.SessionId
              JOIN Module m ON s.ModuleId = m.ModuleId
              WHERE
              (st.StudentUsername = '".mysql_real_escape_string($studentid)."')
              ORDER BY c.CourseName, st.StudentUsername, m.ModuleName, s.SessionId
              ";
    
            $num = mysql_num_rows($result = mysql_query($query));
            mysql_close();
    
        ?>
    
          <p>
          Your Search:
          </p>
          <p>
          <strong>Student Username:</strong> <?php echo "'$studentid'"; ?><br/>
        </p>
         <?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; 
            $weightSession = 0;
            $courseTotal = 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"; 
                } 
        } 
      
        
        
            echo "<p><br><strong>Course:</strong> {$courseId} - {$courseName} <strong>Course Mark:</strong></p><br>\n";
            
            $moduleHTML = "<p><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($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($courseId, $courseName, $moduleId, $moduleName, $sessionsAry); 
                } 
        
                //Reset sessions data array and Set values for new module 
        
                $sessionsAry = array(); 
                $moduleId    = $row['ModuleId']; 
                $moduleName  = $row['ModuleName']; 
                $courseName  = $row['CourseName'];
                $courseId = $row['CourseId']; 
            } 
                 
            //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($courseId, $courseName, $moduleId, $moduleName, $sessionsAry); 
        
        //Display the output 
        echo $output; 
          }
      }
        ?>
Thank you