css 如何获取一行的平均值,将其插入到数据库表中并取出

7gyucuyw  于 2023-04-23  发布在  其他
关注(0)|答案(2)|浏览(73)

我有这个代码打印出我的数据库表的值,我希望它有平均值

<?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的所有值相加。

9fkzdhlc

9fkzdhlc1#

在while循环外添加一个total变量:

$total = 0;

循环内部:

$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'];

while循环之后:

$average = $total / 22;

然后你可以插入到db中:

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

最后,将其恢复:

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

使用值:

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

rsaldnfx2#

不是在PHP中计算,而是可以在DB本身中计算,这将最小化PHP和DB之间的往返。
下面的例子将满足您的要求,但在插入/更新表时要注意。如果您从用户接收数据,请不要在没有转义值的情况下插入/更新。

<?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);
?>

相关问题