I have this code for printing out the values of my database table and I want it to have the average
<?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'); ?>
My database table looks the picture attached
How to add up all the values of the whole row with all columns
then fetch it out so I can print it out along with the other inside the code I have
I have searched everywhere and all I see is getting the average of the column of a database table so I was wondering how can I add up all the values of column q1 to q22 of a row.
2
Answers
Add a total variable outside the while loop:
Inside the loop:
After the while loop:
Then you can insert into the db:
And finally retrieve it:
use the value:
Instead of calculating in PHP, you can calculate in DB itself, which will minimize the roundtrips between PHP and DB.
The following example will suffice your requirement, but care to be take while inserting / updating the table. Do not insert / update without escaping the values if you are receiving the data from user.