skip to Main Content

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


  1. 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>
    
    Login or Signup to reply.
  2. 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.

    <?php 
    include('includes/header.php');
    include_once('../config/dbcon.php');
    
    // Change your query as shown below. this is very basic query, 
    // you can improve this query to get column names dynamically.
    $query="
    SELECT 
       *, 
       (
        (q1+q2+q3+q4+q5+q6+q7+q8+q9+q10+q11+q12+q13+q14+q15+q16+q17+q18+q19+q20+q21+q22)/22 
       ) AS AVG 
    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['AVG']; //<--CHANGE HERE THE AVG COLUMN NAME ?> </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 
              // You cannot insert the data into already existing row, 
              // in that case you have to UPDATE the row as below
              // (change primary key as per your db structure in `WHERE` condition).
    
               $query="UPDATE testeval SET ave = $row['AVG'] WHERE ID = $row['ID']"; 
               mysqli_query($con,$query);
          } 
         ?> 
    
        </table> 
        </body> 
        </html>
    
    <?php include('includes/footer.php'); 
    
    // IMPORTANT :- do not forget to close this connection and re-open when u require in other pages.
    mysqli_close($con);
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search