Got explanation what I need to do but don't know how to code it

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 :

`echo "<p><br><strong>Course:</strong> {$courseId} - {$courseName} <strong>Course Mark:</strong></p><br>\
";`

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:

    &lt;?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();
    
        ?&gt;
    
          &lt;p&gt;
          Your Search:
          &lt;/p&gt;
          &lt;p&gt;
          &lt;strong&gt;Student Username:&lt;/strong&gt; &lt;?php echo "'$studentid'"; ?&gt;&lt;br/&gt;
        &lt;/p&gt;
         &lt;?php
         
         if($num ==0){
        echo "&lt;p&gt;Sorry, No Records were found from this Search&lt;/p&gt;";}
        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 .= "&lt;p&gt;&lt;strong&gt;Session:&lt;/strong&gt; {$session['SessionId']} &lt;strong&gt;Session Mark:&lt;/strong&gt; {$session['Mark']}&lt;/strong&gt; &lt;strong&gt;Session Weight Contribution&lt;/strong&gt; {$session['SessionWeight']}%&lt;/p&gt;\
"; 
                
                $markTotal += round($session['Mark'] / 100 * $session['SessionWeight']); 
                $weightSession  += ($session['SessionWeight']); 
                $markGrade = round($markTotal /  $weightSession * 100); 
                     
                if ($markGrade &gt;= 70) 
                { 
                    $grade = "A"; 
                } 
        
                else if ($markGrade &gt;= 60 && $markGrade &lt;= 69) 
                { 
                    $grade = "B"; 
                } 
        
                else if ($markGrade &gt;= 50 && $markGrade &lt;= 59) 
                { 
                    $grade = "C"; 
                } 
        
                else if ($markGrade &gt;= 40 && $markGrade &lt;= 49) 
                { 
                    $grade = "D"; 
                } 
        
                else if ($markGrade &gt;= 30 && $markGrade &lt;= 39) 
                { 
                    $grade = "E"; 
                } 
        
                else if ($markGrade &gt;= 0 && $markGrade &lt;= 29) 
                { 
                    $grade = "F"; 
                } 
        } 
      
        
        
            echo "&lt;p&gt;&lt;br&gt;&lt;strong&gt;Course:&lt;/strong&gt; {$courseId} - {$courseName} &lt;strong&gt;Course Mark:&lt;/strong&gt;&lt;/p&gt;&lt;br&gt;\
";
            
            $moduleHTML = "&lt;p&gt;&lt;strong&gt;Module:&lt;/strong&gt; {$moduleId} - {$moduleName} &lt;strong&gt;Module Mark:&lt;/strong&gt; {$markTotal} &lt;strong&gt;Mark Percentage:&lt;/strong&gt; {$markGrade} &lt;strong&gt;Grade:&lt;/strong&gt; {$grade} &lt;/p&gt;\
"; 
        
            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'=&gt;$row['SessionId'], 'Mark'=&gt;$row['Mark'], 'SessionWeight'=&gt;$row['SessionWeight']); 
                 
        }    //Get output for last module 
        
        $output .= outputModule($courseId, $courseName, $moduleId, $moduleName, $sessionsAry); 
        
        //Display the output 
        echo $output; 
          }
      }
        ?&gt;

Thank you

What should the output look like?

The output should look like this below:

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%

Try loading the data in a multidimensional array first, and the loop through the array to display the data:


        // load query data in a multidimensional array
        $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'];
        }

        // 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);

        // loop through the array and create the output
        $output = ""; 
        foreach ($dataArray as $courseId => $courseData) { 
           // elaborate course data
           output .= '<p><br><strong>Course:</strong> ' . $courseId . ' - ' . $courseData['courseName'] . ' <strong>Course Mark:</strong></p><br>\
';

           foreach ($courseData['Modules'] as $moduleId => $moduleData) {
             // elaborate module data
             $output .= '<p><strong>Module:</strong> ' . $moduleId . ' - ' . $moduleData['moduleName'];

             $markTotal = 0; 
             $markGrade = 0; 
             $weightSession = 0;
             $grade = ""; 
             $sessionsHTML = ""; 
             foreach ($moduleData['Sessions'] as $sessionId => $sessionData) {
                // elaborate session data
                $markTotal += round($sessionData['Mark'] / 100 * $sessionData['SessionWeight']); 
                $weightSession  += ($sessionData['SessionWeight']); 
                $sessionsHTML .= '<p><strong>Session:</strong> ' . $sessionId . ' <strong>Session Mark:</strong> ' . $sessionData['Mark'] . '</strong> <strong>Session Weight Contribution</strong> ' . $sessionData['SessionWeight'] . '%</p>\
'; 
             }
             $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"; } 

             $output .= ' <strong>Module Mark:</strong> ' . $markTotal . ' <strong>Mark Percentage:</strong> ' . $markGrade . ' <strong>Grade:</strong> ' . $grade . ' </p>\
';  
             $output .= $sessionsHTML;
           }  // <-- end of sessions foreach
         }  // <-- end of modules foreach
       }  // <-- end of courses foreach

       //Display the output 
       echo $output; 

Thank you very much, that has worked very well as it does show the course details only once. The print_r does show clearly how the query works and I now know how what that person meant by initializing an multi dimensional array in a loop. Thank you very much and thank you for devoting your time to help me :slight_smile: