2011-11-03 170 views
0

我在我的php中有一个计算问题。我想补充了所有的会议标记($行[“马克”]}每个模块,并显示在每个模块名称的末尾每个模块的答案下面是我的代码:PHP计算没有显示我想要的正确答案?

$output = ""; 
$studentId = false; 
$courseId = false; 
$moduleId = false; 

    //BELOW IS THE CALCULATION IN PHP 

    $moduletotal = 0; 

    while ($row = mysql_fetch_array($result)){  

     $moduletotal += $row['Mark']; 

     $modulemark = (int)($moduletotal); 

//BELOW IS HOW PHP WILL DISPLAY THE RESULT 
($modulemark is at the end of the 3rd (last) if statement) 

    if($studentId != $row['StudentUsername']) {   

     $studentId = $row['StudentUsername'];   
     $output .= "<p><strong>Student:</strong> {$row['StudentForename']} {$row['StudentSurname']} ({$row['StudentUsername']})"; 

     }  

     if($courseId != $row['CourseId']) {  

     $courseId = $row['CourseId'];    
     $output .= "<br><strong>Course:</strong> {$row['CourseId']} - {$row['CourseName']} <br><strong>Year:</strong> {$row['Year']}</p>";  

     }  

    if($moduleId != $row['ModuleId']) {   

     $moduleId = $row['ModuleId'];   
     $output .= "<p><br><strong>Module:</strong> {$row['ModuleId']} - {$row['ModuleName']} $modulemark</p>"; 

     } 

     $output .= "<p><strong>Session:</strong> {$row['SessionId']} {$row['Mark']}</p>"; 



     }    

    echo $output; } 

BELOW是结果它显示在浏览器上:

Student: Mayur Patel (u0867587) 
Course: INFO101 - Bsc Information Communication Technology 
Year: 3 


Module: CHI2550 - Modern Database Applications 72 (72 is the answer to the calculation for the first module but this is incorrect at it should also add the 67 and thus become 139) 

Session: AAB 72 

Session: AAE 67 

Module: CHI2513 - Systems Strategy 200 (200 is the answer to the calculation for this module but this is incorrect it should be only 61. But what it has done is that it has added the 72 and 67 from the first module and added it with the 61 in this module) 

Session: AAD 61 

SO WHAT MY的问题是,如何找到每个模块MARK其中可变$ modulemark =(int)的($ moduletotal)的总;试图DO通过在每个模块中添加所有会话标记

我曾尝试在q中使用SUM(gr.Mark) uery和使用GROUP BY SessionId,ModuleId,StudentUsername和CourseId,但最终无法显示任何记录。这就是为什么我想用php而不是SQL来计算。

下面是查询,如果你想看到IT:

$query = " 
       SELECT c.CourseName, 
st.CourseId, st.Year, st.StudentUsername, st.StudentForename, st.StudentSurname, 
m.ModuleName, m.Credits, 
s.ModuleId, 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 
       "; 
+4

你为什么向我们大喊大叫? – DampeS8N

+0

你是什么意思呐喊,我把它放在首都,所以你更容易阅读我的问题 – BruceyBandit

+1

[不,它是喊。](http://en.wikipedia.org/wiki/All_caps#Computing) – DampeS8N

回答

1

很明显,你是不是重置$moduletotal 0模块的变化(或其他)时。您需要检测何时停止求和,发射和/或存储结果,然后重置计数器。

+0

我设法找出答案,谢谢 – BruceyBandit