How to display addition/ sum of number field, in repeator?
I have created a field group and named as ‘payments’, I created one field named as ‘information’, now I created four subfileds and diaplayed in a table as
<table class=”table table-striped”>
<thead>
<tr>
<th>Client</th>
<th>Amount Paid</th>
<th>Payment Date</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<?php if(get_field(‘information’)): ?> <?php while(has_sub_field(‘information’)): ?>
<tr>
<td><?php the_sub_field(‘client’); ?></td>
<td><?php the_sub_field(‘amount_paid’); ?></td>
<td><?php the_sub_field(‘payment_date’); ?></td>
<td><?php the_sub_field(‘notes’); ?></td>
</tr>
<?php endwhile; ?>
<?php endif; ?>
</tbody>
</table>
Here ‘<?php the_sub_field(‘amount_paid’); ?>’ is a number field, I want to display addition/ sum of all the values so user will come to know what is the total income without counting the values.
I tried this
<?php
$total = 0;
while(the_repeater_field(‘information’ )):
the_sub_field(‘amount_paid’ );
$total += intval( get_sub_field(‘amount_paid’ ) );
endwhile;
echo $total;
?>
But its showing all numbers together, not as total!
It gave me output as 1000012707, should be 10000+12707
What version of ACF are you using?
What type of field is it?
Is it a number field or something else?
If it’s not a number field, what is the data in the field?
Are you manipulating
I used this and its working now, Thanks for your time!
<?php
$total = 0;
while(the_repeater_field(‘information’ )):
$total += intval( get_sub_field(‘amount_paid’ ) );
endwhile;
echo $total;
?>
Tell, please, how to modify this code if field values contain decimals?
Ex.: 3.333+2.333=5.666 ($total must be 5.666)
Thanks!
replace intval
with floatval