簡體   English   中英

PHP計算未顯示我想要的正確答案?

[英]PHP calculation is not displaying the correct answer I want?

我的PHP中有一個計算問題。 我想為每個模塊加所有會話標記($ 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
              ";

很明顯,當模塊更改(或其他任何操作)時,您沒有將$moduletotal重置$moduletotal 0。 您需要檢測何時停止求和,發出和/或存儲結果,然后重置計數器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM