how to get average of a row, insert it to the database table and fetch it out

Advertisements

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.

hoping someone can help me out 🙂

>Solution :

Add a total variable outside the while loop:

$total = 0;

Inside the loop:

$total += $row['q1'] + $row['q2'] + $row['q3'] + $row['q4'] + $row['q5'] + $row['q6'] + $row['q7'] + $row['q8'] + $row['q9'] + $row['q10'] + $row['q11'] + $row['q12'] + $row['q13'] + $row['q14'] + $row['q15'] + $row['q16'] + $row['q17'] + $row['q18'] + $row['q19'] + $row['q20'] + $row['q21'] + $row['q22'];

After the while loop:

$average = $total / 22;

Then you can insert into the db:

include_once('../config/dbcon.php'); 
$query="INSERT INTO testeval (ave) VALUES ('$average')"; 
mysqli_query($con,$query);

And finally retrieve it:

include_once('../config/dbcon.php'); 
$query="SELECT ave FROM testeval"; 
$result=mysqli_query($con,$query); 
$row = mysqli_fetch_assoc($result);
$average = $row['ave'];

use the value:

<td><?php echo $average; ?> </td>

Leave a ReplyCancel reply