1
votes

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

enter image description here

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.

enter image description here

  • 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++;
1

1 Answers

1
votes

Referring to my fields structure below:

Field Name        Field Key           Type
product_codes     field_5b4d8f7246886 Repeater
  - table         field_5b4d8fd946888 Repeater
    - information field_5b4d906246889 Repeater
      - text      field_5b4d907f4688a Text

Here's how you can update the first information row in the first table:

  • Using update_sub_row():
    // Here, we build the ancestors, starting from the TOPMOST LEVEL down to the
    // field that we're updating its value.
    $selector = [
        'field_5b4d8f7246886', 1, // selects the first `table` in `product_codes`
        'field_5b4d8fd946888',    // selects ALL the `information` rows
    ];
    
    update_sub_row( $selector, 1, [
        'field_5b4d906246889' => [
            [ 'field_5b4d907f4688a' => '040-811' ],
            [ 'field_5b4d907f4688a' => '040-821' ],
            [ 'field_5b4d907f4688a' => 'Standard Size Fibre Optic Handle' ],
            [ 'field_5b4d907f4688a' => '1' ],
        ],
    ] );
    

  • Using update_sub_field():
    // Here, we build the ancestors, starting from the TOPMOST LEVEL down to the
    // field that we're updating its value.
    $selector = [
        'field_5b4d8f7246886', 1, // selects the first `table` in `product_codes`
        'field_5b4d8fd946888', 1, // selects the first `information` row
        'field_5b4d906246889',    // finally, selects all `text` columns
    ];
    
    update_sub_field( $selector, [
        [ 'field_5b4d907f4688a' => '040-811' ],
        [ 'field_5b4d907f4688a' => '040-821' ],
        [ 'field_5b4d907f4688a' => 'Standard Size Fibre Optic Handle' ],
        [ 'field_5b4d907f4688a' => '1' ],
    ] );
    

  • I hope that helps you, and do let me know if you need further assistance.


    UPDATE

    Try this code:

    $the_row = [
        'field_5ae0882f9d6f9', $tablecounter,
        'field_5b3f409de191a',
    ];
    
    //$value = array();
    $rowcount = 1;
    foreach ( $rows as $row ) {
    
        $cells = $row->find('td');
        //$columnsCount = count($cells);
        //$counter = 1;
        $cols = [];
        foreach ($cells as $cell) {
            $value = strip_tags($cell->innertext);
            $cols[] = [ 'field_5ae088b79d6fc' => $value ];
            //$counter++;
        }
    
        update_sub_row( $the_row, $rowcount, [
            'field_5ae088999d6fb' => $cols,
        ], $post_id );
    
        $rowcount++;
    
    }
    
    $tablecounter++;