0

I have the following loop which shows the fields money and percent. I want to add all the money together as it iterates through, how can I do this and echo out a total outside the loop?

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

2 Answers 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;
Sign up to request clarification or add additional context in comments.

Comments

0

Edited to reflect the new format of the question. Here is the answer:

<?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";
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.