Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6796861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:32:19+00:00 2026-05-26T18:32:19+00:00

I managed to sort out the calculation but there is just one last question

  • 0

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:

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%

Course: INFO101 – Bsc Information Communication Technology Course Mark: 65

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

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%

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.

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?

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 .= "<strong>Student:</strong> {$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 => $courseData) {  
           // elaborate course data 

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

           $courseHTML = ""; 

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

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

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

             $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 .= "<table><tr><th>Session</th><th>Session Mark</th><th>Session Weight</th></tr><tr><td>" . $sessionId . "</td><td>" . $sessionData['Mark'] . "</td><td>" . $sessionData['SessionWeight'] ."%</td></tr></table>\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 >= 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"; }              

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

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

         $courseHTML .= " <th>Course Mark:" . $courseGrade . "</th></tr></table>";

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

       }  // <-- end of courses foreach
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:32:19+00:00Added an answer on May 26, 2026 at 6:32 pm

    Of course, just change the order you echo your strings:

    $output .= $courseHTML; adds $courseHTML to the end of output. Using $output = $courseHTML . $output; adds $courseHTML to the start of $output.

    Make sure your HTML is still valid!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is sort of the next step of the LINQ to DB2 question I
I know that this might be a duplicate of this question but that was
First of all, i know this question has been sort of asked/sort-of answered here:
I've managed to get my pthreads program sort of working. Basically I am trying
I managed to get some help from a previous question and I have the
I managed to get the YUI calendar widget working fine. But When it displays
I have a class that is a manager sort of class. One of it's
Y.A.N.Q. (yet another n00b question). I have managed to design my db, defining tables
ok I was going to edit my previous question but i wasnt sure if
I have sort of a table with a radio-button column. I managed to make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.