简体   繁体   English

PHP foreach while 循环如何对输出求和

[英]PHP foreach while loop how to sum output

Query returns sum of elements group by operator (selected by checkbox).查询返回按运算符分组的元素总和(由复选框选择)。

How to sum up all that values?如何总结所有这些价值? I tried using array_sum() but didn't worked or maybe i am not using this function correct.我尝试使用 array_sum() 但没有用,或者我没有正确使用这个函数。 Thank you谢谢

<?php

if(isset($_POST['delete']))
{
$ziua=$_POST["date"];
}
else 
{
    $ziua=date('Y-m-d');
}
if(isset($_POST['delete']))
{//check to see if the delete button has been pressed
    if(isset($_POST['box']))
    { //check to see if any boxes have been checked 
        $num = 0;//used to count the number of rows that were deleted
        $box = $_POST['box'];
        foreach ($box as $key =>$val) 
        { //loop through all the checkboxes
                  $num++;

              $sqldel=" SELECT U.username , SUM(L.geometrie1) A from list L,users U where L.user_id='$val' 
              and L.date_posted like '%$ziua%' AND L.user_id=U.id group by U.username  ";//delete any that match id
              $resdel=mysql_query($sqldel);//send the query to mysql

            while($row = mysql_fetch_array($resdel))
            {
            Print "<tr>";
            Print '<td align="center">'. $row['0']. "</td>";
            Print '<td align="center">'. $row['1']. "</td>";
            Print "</tr>";
            }
         }
    }
}
?>
<!-- end snippet -->

Before you loop add在循环之前添加

 $total=0;

In your loop add在你的循环中添加

  $total += $row[1];

Then echo it after the loop然后在循环后回显

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM