0

我有以下循环显示字段金钱和百分比。我想在迭代时将所有钱加在一起,我该如何做到这一点并在循环外回显一个总数?

while(the_repeater_field('income')) {
   the_sub_field('money');
   the_sub_field('percent');
}
4

2 回答 2

1
<?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;
于 2012-10-16T08:49:03.863 回答
0

编辑以反映问题的新格式。这是答案:

<?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";
?>
于 2012-10-16T08:47:56.880 回答