u0867587 Posted November 7, 2011 Share Posted November 7, 2011 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) { 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; } Quote Link to comment https://forums.phpfreaks.com/topic/250655-i-want-echo-to-be-displayed-once-in-foreach-loop/ Share on other sites More sharing options...
ocpaul20 Posted November 8, 2011 Share Posted November 8, 2011 Why can't you set a flag once you have displayed the required output once and then test that flag before displaying again? Quote Link to comment https://forums.phpfreaks.com/topic/250655-i-want-echo-to-be-displayed-once-in-foreach-loop/#findComment-1286079 Share on other sites More sharing options...
cyberRobot Posted November 8, 2011 Share Posted November 8, 2011 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: http://www.google.com/search?q=php+array_unique+multidimensional+array Quote Link to comment https://forums.phpfreaks.com/topic/250655-i-want-echo-to-be-displayed-once-in-foreach-loop/#findComment-1286191 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.