我有以下循环显示字段金钱和百分比。我想在迭代时将所有钱加在一起,我该如何做到这一点并在循环外回显一个总数?
while(the_repeater_field('income')) {
the_sub_field('money');
the_sub_field('percent');
}
我有以下循环显示字段金钱和百分比。我想在迭代时将所有钱加在一起,我该如何做到这一点并在循环外回显一个总数?
while(the_repeater_field('income')) {
the_sub_field('money');
the_sub_field('percent');
}
<?php
$Sum_Money = 0;
$Sum_Percent = 0;
while (the_repeater_field('income')) {
$Money = the_sub_field('money');
$Percent = the_sub_field('percent');
$Sum_Money += $Money;
$Sum_Percent += $Percent;
}
echo $Sum_Money;
编辑以反映问题的新格式。这是答案:
<?php
$total = 0;
$totalPercent = 0;
while(the_repeater_field('income')) {
$total += the_sub_field('money');
$totalPercent += the_sub_field('percent');
}
echo "Total: $total, Percent: $totalPercent";
?>