Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sum of a calculated column in foreach

Tags:

php

mysql

I have a mysql table

service_charges

id service_name service_charges 

I use a mysql query like

select service_name service, service_charges,count(service_name)as unit 
from service charges group_by service

in php file I am using

foreach(services as service){
echo $service['service'];
echo $service['service_charges'];
echo $service['unit'];
$service_charges_total = (($service['service_charges'])*($service['unit']));
echo $sevice_charges_total;
}

I am getting the result like this: enter image description here

Now How can I get the total for $service_charges_total(amount column) i.e. I want a total of 4200+1200 + 350 = 5750.

Thanks.

like image 451
Pawan Avatar asked Oct 19 '22 20:10

Pawan


1 Answers

You can do it at php at your function like this

$total=0;
foreach(services as service)
{
   echo $service['service'];
   echo $service['service_charges'];
   echo $service['unit'];
   $service_charges_total = (($service['service_charges'])*($service['unit']));
   echo $sevice_charges_total;
   $total+=$sevice_charges_total;
}
echo $total;
like image 176
Shaiful Islam Avatar answered Oct 27 '22 00:10

Shaiful Islam