【问题标题】:PHP calculation is not displaying the correct answer I want?PHP 计算没有显示我想要的正确答案?
【发布时间】:2011-12-21 05:18:16
【问题描述】:

我的 php.ini 中有一个计算问题。我想为每个模块添加所有会话标记($row['Mark']},并在每个模块名称的末尾显示每个模块的答案。下面是我的代码:

$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;    }

以下是它在浏览器上显示的结果:

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

所以我的问题是如何找到变量 $modulemark = (int)($moduletotal); 的每个模块标记的总和正在尝试通过将每个模块中的所有会话标记相加来实现。

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

如果您想查看以下是查询:

  $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
              ";

【问题讨论】:

  • 大喊大叫是什么意思,我把它放在大写字母中,这样大家更容易阅读我的问题

标签: php mysql sql database


【解决方案1】:

很明显,当模块更改(或其他)时,您不会将 $moduletotal 重置为 0。您需要检测何时停止求和、发出和/或存储结果,然后重置计数器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    相关资源
    最近更新 更多