我在foreach循环中的当前输出是这样的:
我想为每个“ID”添加“金额”,并在结尾输出每个“ID”的总数。 “ID”的数量是动态的。这是我目前的代码:
$amount_for_that_group="0";
foreach ($beneficiaryIDArray as $beneficiaryID) {
//see what distribution group they belong to
$query_ben_group= "SELECT * FROM `beneficiaries` WHERE `beneficiary_id`='$beneficiaryID'";
$rs_ben_group=$conn->query($query_ben_group);
if($rs_ben_group === false){ trigger_error('SQL error:'.$query_ben_group.' Error: '.$conn->error, E_USER_ERROR);}
else{
while ($row_ben_group=$rs_ben_group->fetch_assoc()) {
$ggroup=$row_ben_group['reward_group'];
if ($ggroup==1) {
$amount_for_that_group=$single_group1_amount;
}
if ($ggroup==2) {
$amount_for_that_group=$single_group2_amount;
}
if ($ggroup==3) {
$amount_for_that_group=$single_group3_amount;
}
}
}
echo "ID:$beneficiaryID Amount: $amount_for_that_group<br>";
}