Support

Account

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