php - 如何对数据库表中的列进行求和,但将通过 3 个不同的行组获得总结果

标签 php mysql rows

我在数据库中有一个表,其中条件列包含 24 行(条件 1 到条件 24)每个条件行的百分比结果..

问题是,我想制作一个在表格中显示的公式,如下所示:-

  1. 标准 1 到 9,总百分比是多少,
  2. 然后是标准10到15,总共占多少百分比,
  3. 最后,对于标准 16 至 24,百分比总计是多少。

计数后,它将被插入到数据库中的另一个表中。

这一切......

我只是编程新手..抱歉,非常感谢你...

最佳答案

注释里面有解释/* */:

<html>
<body>

<?php

$YourConnection=mysqli_connect("YourHost","YourUsername","YourPassword","YourDatabase");

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

if(isset($_POST['submit'])){ /* IF SUBMIT */

$total1=0;
$total2=0;
$total3=0;

$criteria=$_POST['criteria']; /* GET THE INPUT */

for($y=0;$y<=8;$y++){ 
$total1=$total1+$criteria[$y]; /* Get total of first 1-9 */
}

for($z=9;$z<=14;$z++){ 
$total2=$total2+$criteria[$z]; /* Get second total 10-15 */
}

for($a=15;$a<=23;$a++){
$total3=$total3+$criteria[$a]; /* Get last total 16-24 */
}

echo "Criteria 1 to 9: Total Percentage is ".$total1."<br>";
echo "Criteria 10 to 15: Total Percentage is ".$total2."<br>";
echo "Criteria 16 to 24: Total Percentage is ".$total3."<br>";

mysqli_query($YourConnection,"INSERT INTO yourtable ('column1','column2','column3') VALUES ('$total1','$total2','$total3')");

} /* END OF IF ISSET SUBMIT */

echo "<form action='' method='POST'>"; /* SUBMIT ON SELF */

for($x=0;$x<=23;$x++){

if($x==0){
echo "<h4>Criteria From 1-9</h4>";
}
if($x==9){
echo "<h4>Criteria From 10-15</h4>";
}
if($x==15){
echo "<h4>Criteria From 16-24</h4>";
}

$placenumber=$x+1;
$placeholder="Criteria ".$placenumber;

echo "<input type='number' name='criteria[$x]' placeholder='$placeholder'><br>"; /* FIX 24, RIGHT? */
} /* END OF FOR LOOP */

echo "<input type='submit' name='submit'>";
echo "</form>";

?>

</body>
</html>

此代码已经过测试。你也应该这样做。

关于php - 如何对数据库表中的列进行求和,但将通过 3 个不同的行组获得总结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22551032/

相关文章:

java - 如何配置 Spring Boot 以使用两个数据库?

arrays - 获取数组的整行

php - 如何将filter_input与数组一起使用?

php - Laravel OrderBy 关系计数与分页

javascript - 如何使用php在mysql数据库中存储文件和字段数组

java - 使用 GUI 和 MySQL 数据库制作程序

mysql每张表的数据量

php - 如何将多个文件上传插入到mysql中

php - 在 php 中使用 mysql 将日期保存在数据库中

php - WooCommerce 中产品下的相关产品文本太高?