#php #mysql #sql #database
#php #mysql #sql #База данных
Вопрос:
У меня проблема с вычислением в моем 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
";
Комментарии:
1. что значит кричать, я написал это заглавными буквами, чтобы вам всем было легче прочитать мой вопрос
Ответ №1:
Очевидно, что вы не сбрасываете $moduletotal
значение 0 при изменении модуля (или что-то еще). Вам нужно определить, когда прекратить суммирование, выдать и / или сохранить ваш результат, а затем сбросить счетчик.