Support

Account

Home Forums Front-end Issues Programmatically update nested repeater values

Solving

Programmatically update nested repeater values

  • I’m trying to take the following data from a table:

    Table

    And input it to a nested ACF repeater field. I’ve got very close in that it’s creating the correct amount of tables (3 from the example), the correct amount of columns for each table.

    The last part isn’t quite working, it’s only inputting the last row of data into the “Information” repeater, which suggests it’s not iterating the row numbers, therefore just inputting it to row 1.

    **Where am I going wrong (see code at the bottom)? So for the first table, each information table should have 4 rows of data in it for each column.**

    ACF

    – Top repeater (product codes): field_5ae0882f9d6f9
    – Nested repeater 1 (table): field_5b3f409de191a
    * Nested repeater 2 (information): field_5ae088999d6fb
    * Field to update (text): field_5ae088b79d6fc

    Here’s the code:

             $value = array();
              $rowcount = 1;
              while($row = next($rows)){
            
                  $cells = $row->find('td');
                  $columnsCount = count($cells);
                  $counter = 1;
                  foreach ($cells as $cell) {
                    $value = array(
                      "field_5ae088999d6fb" => array(
                        array("field_5ae088b79d6fc" => strip_tags($cell->innertext))
                      )
                    );
                    update_sub_row( array('field_5ae0882f9d6f9', $tablecounter, 'field_5b3f409de191a'), $counter, $value, $post_id );
                    $value = array();
                    $counter++;
                  }
            
                $rowcount++;
            
          }
        
          $tablecounter++;
  • I can’t say, but maybe using update_sub_row is the problem, since the row you’re trying to update does not exist, to be honest I’m not sure how it works. Maybe try using add_sub_row() https://www.advancedcustomfields.com/resources/add_sub_row/

  • Doesn’t seem to add anything in the second repeater using that. The only way I can get it to add rows is using the code in my question. It feels extremely close but it’s just not iterating through the last repeater, so the first row is always copied over.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Programmatically update nested repeater values’ is closed to new replies.