我有这个代码打印出我的数据库表的值,我希望它有平均值
<?php include('includes/header.php'); ?>
<?php
include_once('../config/dbcon.php');
$query="select * from testeval";
$result=mysqli_query($con,$query);
?>
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td{
border: 3px solid black;
border-collapse: collapse;
}
th{
padding: 5px;
text-align: left;
font-weight: bold;
}
td {
text-align: center;
}
.title {
max-width: 100%;
margin: auto;
}
table {
text-align= center;
border: 1px;
max-width= 1000px;
line-height= 40px;
}
</style>
</head>
<body class = "title">
<table>
<form>
<tr>
<th colspan="4"><h2>Evaluation Record</h2></th>
</tr>
<?php
while($row =mysqli_fetch_assoc($result))
{
?>
<tr>
<th> ID: </th>
<td><?php echo $row['id']; ?> </td>
</tr>
<tr>
<th> Name: </th>
<td><?php echo $row['name']; ?> </td>
</tr>
<tr>
<th> 1. Formulates/adopts objectives of the syllabus course learning outcomes. </th>
<td><?php echo $row['q1']; ?> </td>
<tr>
<th> 2. Selects content and prepares appropriate instructional materials/teaching aids. </th>
<td><?php echo $row['q2']; ?> </td>
</tr>
<tr>
<th> 3. Selects appropriate teaching methods/strategies. </th>
<td><?php echo $row['q3']; ?> </td>
</tr>
<tr>
<th> 4. Relates new lesson with previous knowledge/skills. </th>
<td><?php echo $row['q4']; ?> </td>
</tr>
<tr>
<th> 5. Conveys ideas clearly. </th>
<td><?php echo $row['q5']; ?> </td>
</tr>
<tr>
<th> 6. Utilizes the art of questioning to develop higher level of thinking. </th>
<td><?php echo $row['q6']; ?> </td>
</tr>
<tr>
<th> 7. Ensures students participation. </th>
<td><?php echo $row['q7']; ?> </td>
</tr>
<tr>
<th> 8. Shows mastery of the subject matter. </th>
<td><?php echo $row['q8']; ?> </td>
</tr>
<tr>
<th> 9. Utilizes the blackboard or the learning management system of the college. </th>
<td><?php echo $row['q8']; ?> </td>
</tr>
<tr>
<th> 10. Creates assessments that are aligned with the syllabus course learning outcomes </th>
<td><?php echo $row['q10']; ?> </td>
</tr>
<tr>
<th> 11. Evaluates the attainment of the syllabus course learning outcomest. </th>
<td><?php echo $row['q11']; ?> </td>
</tr>
<tr>
<th> 12. Maintains orderly classroom that is conducive to learning. </th>
<td><?php echo $row['q12']; ?> </td>
</tr>
<tr>
<th> 13. Decisiveness </th>
<td><?php echo $row['q13']; ?> </td>
</tr>
<tr>
<th> 14. Honesty / Integrity </th>
<td><?php echo $row['q14']; ?> </td>
</tr>
<tr>
<th> 15. Dedication / Commitment </th>
<td><?php echo $row['q15']; ?> </td>
</tr>
<tr>
<th> 16. Initiative / Resourcefulness </th>
<td><?php echo $row['q16']; ?> </td>
</tr>
<tr>
<th> 17. Courtesy </th>
<td><?php echo $row['q17']; ?> </td>
</tr>
<tr>
<th> 18. Human Relations </th>
<td><?php echo $row['q18']; ?> </td>
</tr>
<tr>
<th> 19. Leadership </th>
<td><?php echo $row['q19']; ?> </td>
</tr>
<tr>
<th> 20. Stress Toleranc </th>
<td><?php echo $row['q20']; ?> </td>
</tr>
<tr>
<th> 21. Fairness / Justice </th>
<td><?php echo $row['q21']; ?> </td>
</tr>
<tr>
<th> 22. Proper Attire / Good Grooming </th>
<td><?php echo $row['q22']; ?> </td>
</tr>
<tr>
<th> Average: </th>
<td><?php echo $row['ave']; ?> </td>
</tr>
<tr>
<th> Remarks: </th>
<td><?php echo $row['message']; ?> </td>
</tr>
<tr>
<th> <center> ------------------------------------------------------ </center> </th>
<td> <center> ------------------------------------------------------ </center> </td>
</tr>
<tr>
<th> <center> ------------------------------------------------------ </center> </th>
<td> <center> ------------------------------------------------------ </center> </td>
</tr>
</tr>
</tr>
</form>
<?php
}
?>
</table>
</body>
</html>
<?php include('includes/footer.php'); ?>
我的数据库表看起来附上图片
如何将整行的所有值与所有列相加,然后将其取出,以便我可以将其与其他代码沿着打印出来,我已经搜索了所有地方,我所看到的是获得数据库表中列的平均值,所以我想知道如何将一行中列q1到q22的所有值相加。
2条答案
按热度按时间9fkzdhlc1#
在while循环外添加一个total变量:
循环内部:
while循环之后:
然后你可以插入到db中:
最后,将其恢复:
使用值:
rsaldnfx2#
不是在PHP中计算,而是可以在DB本身中计算,这将最小化PHP和DB之间的往返。
下面的例子将满足您的要求,但在插入/更新表时要注意。如果您从用户接收数据,请不要在没有转义值的情况下插入/更新。