Home › Forums › General Issues › Hypothetical : How can I add values from a field in a repeater? › Reply To: Hypothetical : How can I add values from a field in a repeater?
I personally like to use output buffers, the third option. I gave multiple options because there’s always multiple ways to do something in PHP depending on you preferred style of coding.
Just outputting the numbers it would look something like this
if (have_rows('progress_barRepeater')) {
// start an output buffer to capture html output
ob_start()
$total = 0; // hold total from rows
$count = 0; // hold count of rows
?>
<ul>
<?php
while(have_rows) {
the_row();
$value = get_sub_field('progress_subPercent');
$total += $value;
$rows++;
?>
<li><?php echo $value</li>
<?php
} // end while have rows
?>
</ul>
<?php
// get the output buffer and store it
$html = ob_get_clean();
// calculate average
$average = $total/$count;
// display the average
echo '<p>Average = ',$average,'</p>';
// display the list
echo $html;
} // end if have rows
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.