Support

Account

Home Forums General Issues Hypothetical : How can I add values from a field in a repeater?

Solving

Hypothetical : How can I add values from a field in a repeater?

  • I am making a Progress Bar module. It’s fairly easy, you add a task name and a value (in a percentage). Straightforward.

    I got the proof of concept working with one bar (the visual magic is in the CSS). Then I realized I wanted to have a Parent Bar and Childen Bars. So that if you want to have Subtasks you can.

    So now I have the Parents as a Repeater, and it has a subRepeater for the children.

    IF the Child/subRepeater has rows. Then I’ll override the value for the parent.

    My plan is to average the children. I found I can use get_row_index() as an incrimentor.

    But where I hit a wall in my hypothetical code is : How do I reach into the repeater, get the “percentage values” (there are only two fields per repeater) and add them before dividing by the increment count (made hypothetically worse as I don’t know how many values the repeater will have)?

    Here’s the best I could do and it displays quite a bit of stuff, I assume this is an array, but I don’t know how to target it and get values to add.

        <?php if(have_rows('progress_barRepeater')):?>  
        
            <?php while(have_rows('progress_barRepeater')): the_row();?>
        
                <?php $rows = get_row('progress_barRepeater'); ?>
                <?php var_dump($rows)?>

    This returns this (formatted for easier reading:

    array(4) { 
       ["progress_name"]=> string(14) "Parent Bar #1!" 
       ["progress_percent"]=> string(2) "75" 
       ["progress_subBool"]=> bool(true) 
       ["progress_subRepeater"]=> array(4) { 
          [0]=> array(2) { 
             ["progress_subName"]=> string(5) "Tacos" 
             ["progress_subPercent"]=> string(2) "75" } 
          [1]=> array(2) { 
             ["progress_subName"]=> string(7) "Napkins" 
             ["progress_subPercent"]=> string(2) "50" } 
          [2]=> array(2) { 
             ["progress_subName"]=> string(5) "Salsa" 
             ["progress_subPercent"]=> string(2) "25" } 
          [3]=> array(2) { 
             ["progress_subName"]=> string(6) "Mouths" 
             ["progress_subPercent"]=> string(2) "13" } 
          } 
       } 
    Parent Bar #1!TacosNapkinsSalsaMouths array(4) { 
       ["progress_name"]=> string(14) "Parent Bar #1!" 
       ["progress_percent"]=> string(2) "75" 
       ["progress_subBool"]=> bool(true)
    }
  • You need to do one of a few things.

    The first is loop though the field twice.

    If you use get_field('progress_barRepeater') this will return the entire acf field as a nested array, you could then loop though the rows of the array to get the values and then you can loop through them again to display each row.

    You can capture the output of a single loop using an object buffer, creating html and collecting the information all at the same time. Then after the loop output the “average” and then the content of the object buffer.

  • I’m thankful for you taking the time to answer my question. I am not as proficent with PHP as I’d like to be. Would you mind sharing some code examples?

    I think if I understand you, the first time I loop: I don’t HAVE to display anything, I can just use this to get some of the values and assign them to variables?

    I still don’t know how to do this well IF I don’t even know how many items I might have.

    I DO know I want to try and target and captures each of the progress_subPercent from progress_subRepeater

  • 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
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Hypothetical : How can I add values from a field in a repeater?’ is closed to new replies.