大师们, 这对你们来说可能是一个简单的问题,到目前为止我还无法弄清楚这个问题。
我想根据用户名和项目名称计算 TABLENAME 中的总金额。下面的代码可以很好地计算它。
$item_name ( name1, name2, name3 , ... nameN)
for ($i=0;$i<=count($item_name);$i++)
{
if ($item_status[$i] != 0) {
$sqloverall=$Db1->query("SELECT SUM(amount) as usramount FROM
TABLENAME WHERE user_name='$user_name' and item_name='$item_name[$i]'");
$temp=$Db1->fetch_array($sqloverall);
$overall[]=$temp[usramount];
This runs successfully . I get values into $overall[] .. I do some extra calcuations with if condition.
if ( condition)
{
//run this code // some calcuations
$profit= ($overall[$i] * 8970 )/100
}
else
{
//run this code // some calcuations
$profit= ($overall[$i] * 1254 )/100
}
}
}
问题:
我还需要将 $overall[]=$temp[usramount];
计算为单个变量。这样我就可以在 for 循环之外显示它。
目前它显示这样的值
$overall[0] $overall[1] $overall[N]
请指导我如何将 $overall[$i]
组合成可以在 for 循环外部显示的单个变量
final output : Calcuate sum of all items based on their status and user_name , some item status might be disabled.
编辑1:
我需要对 $overall[] 求和并在 for 循环外部显示。
$var = SUM($overall[]) ;
这是实际需要的。
即
combine $overall[0] , $overall[1] , $overall[2], $overall[N] ,
将其转换为单个变量作为所有数据的总和并显示它。
最佳答案
如果您需要数组所有元素的总和 - 有 array_sum
功能:
$total = array_sum($overall);
关于php - 根据状态和项目名称计算所有项目的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42735431/