迭代循环并将变量添加到一起

时间:2012-10-16 08:43:13

标签: php while-loop accumulate

我有以下循环显示字段money和percent。我希望在迭代过程中将所有的钱都加在一起,我怎么能这样做并在循环之外回显一下?

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

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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";
?>