Jump to content

I want echo to be displayed once in foreach loop


Recommended Posts

my output is this below:


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 it shows 2 different Modules, it shows the "Course" details twice, I only want the "Course" details appear once as you can see both course details are the same.


How can I display the "Course" Details only once in the foreach loop.


To display the "Course" details I use this code: echo

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



Below is the code for the function and foreach loop:



     function outputModule($courseId, $courseName, $moduleId, $moduleName, $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; 

Link to comment
Share on other sites

Other than the duplicate entries, is "SessionId" ever the same...for different courses? If not, you could use that field when creating the array. If the session ID is already in the array, don't add another.



array_unique() doesn't seem to work for multidimensional arrays, but that may provide a clue. The search results for "php array_unique multidimensional array" might be helpful:


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

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