想要在顶部而不是底部显示课程详细信息

时间:2011-11-09 11:06:25

标签: php

我设法整理了计算,但只有最后一个问题,然后这个页面完成了。唯一的问题是,为了能够得到计算的答案,我必须在底部显示课程细节,所以输出如下所示:

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%

课程:INFO101 - Bsc信息通信技术课程标记:65

我希望课程详细信息在模块上方输出,如下所示:

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%

问题是,如果我将课程详细信息保留在底部,那么计算就可以了。我把它移到顶部,然后计算不起作用,因为$ courseGrade的计算在底部,因此对于课程标记,它将显示为0.

所以我的问题是如何将课程详情移至顶部并仍然显示课程标记的计算答案?

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 个答案:

答案 0 :(得分:1)

当然,只需更改字符串echo的顺序:

$output .= $courseHTML;$courseHTML添加到输出的末尾。使用$output = $courseHTML . $output;$courseHTML添加到$output的开头。

确保您的HTML仍然有效!