简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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