我想要的是echo "<p>Average Mark: $average</p>";
出现在表格上方而不是表格下方。如果我这样做虽然问题是它不会计算平均值,因为回声高于变量。但是如果我移动变量并且在整个表格上方使用while循环,因为我还需要while循环来从查询中选择$row[Mark]'
字段,因为那是将被平均的字段,它会混乱表的结构。如何在不破坏表结构的情况下在表格上方显示$average, $total, $count
等变量和echo "<p>Average Mark: $average</p>";
?
下面是当前代码,您可以在while循环中看到$count++; , $total += $row['Mark'];
,在底部看到$ average变量和echo。
$result = mysql_query($query);
mysql_close();
?>
<table border='1'>
<tr>
<th>Session ID</th>
<th>TeacherUsername</th>
<th>Teacher Name</th>
<th>Module Number</th>
<th>Module Name</th>
<th>Course ID</th>
<th>Course Name</th>
<th>Year</th>
<th>Student Username</th>
<th>Student Name</th>
<th>Mark</th>
<th>Grade</th>
</tr>
<?php
$total = 0;
$count = 0;
while ($row = mysql_fetch_array($result)) {
$count++;
$total += $row['Mark'];
echo "
<tr>
<td>{$row['SessionId']}</td>
<td>{$row['TeacherUsername']}</td>
<td>{$row['TeacherForename']} {$row['TeacherSurname']}</td>
<td>{$row['ModuleId']}</td>
<td>{$row['ModuleName']}</td>
<td>{$row['CourseId']}</td>
<td>{$row['CourseName']}</td>
<td>{$row['Year']}</td>
<td>{$row['StudentUsername']}</td>
<td>{$row['StudentForename']} {$row['StudentSurname']}</td>
<td>{$row['Mark']}</td>
<td>{$row['Grade']}</td>
</tr>";
}
?>
</table>
<?php
$average = (int)($total/$count);
echo "<p>Average Mark: $average</p>";
?>
答案 0 :(得分:0)
将一个div放在空白的表格上方,然后使用javascript填充它
$("#div-you-created").html(<?php echo "Average Mark: $average"; ?>);
答案 1 :(得分:0)
使用PHP完成任务有两种方法:
SELECT * FROM grades
,则可以使用SELECT SUM(Mark) FROM grades
来获取商标的总和。答案 2 :(得分:0)
而不是回显表,将表存储在字符串中,然后回显平均值,然后回显字符串。
答案 3 :(得分:0)
不是回显行,而是将它们保存到变量中。然后在回显您保存的变量之前回显计算值。观察:
$result = mysql_query($query);
mysql_close();
$output += "
<table border='1'>
<tr>
<th>Session ID</th>
<th>TeacherUsername</th>
<th>Teacher Name</th>
<th>Module Number</th>
<th>Module Name</th>
<th>Course ID</th>
<th>Course Name</th>
<th>Year</th>
<th>Student Username</th>
<th>Student Name</th>
<th>Mark</th>
<th>Grade</th>
</tr>
";
$total = 0;
$count = 0;
while ($row = mysql_fetch_array($result)) {
$count++;
$total += $row['Mark'];
$output += "
<tr>
<td>{$row['SessionId']}</td>
<td>{$row['TeacherUsername']}</td>
<td>{$row['TeacherForename']} {$row['TeacherSurname']}</td>
<td>{$row['ModuleId']}</td>
<td>{$row['ModuleName']}</td>
<td>{$row['CourseId']}</td>
<td>{$row['CourseName']}</td>
<td>{$row['Year']}</td>
<td>{$row['StudentUsername']}</td>
<td>{$row['StudentForename']} {$row['StudentSurname']}</td>
<td>{$row['Mark']}</td>
<td>{$row['Grade']}</td>
</tr>";
}
$output += " </table>";
$average = (int)($total/$count);
echo "<p>Average Mark: $average</p>";
echo $output;
?>