Support

Account

Home Forums Front-end Issues Update parent and child repeater programmatically Reply To: Update parent and child repeater programmatically

  • This is what I tried:

    // Product Code Titles
    $contents = $html->find('div[class=product-table]', 0);
    $data = array();
    $subdata = array();
    $rows = $contents->find('tr');
    $counter = 1;
    $field_key = "field_5ae0882f9d6f9";
    $sub_field_key = "field_5ae088999d6fb";
    
    foreach ($rows as $key_row => $row) {
    
          foreach ($row->find('td') as $key_cell => $cell) {
    
            $data[] = array(
              // element for row
              array(
                // field in parent repeater
                'field_5ae0887c9d6fa' => strip_tags($cell->innertext),
    
                // nested repeater
                $sub_field_key => array(
                  // element for row in nested repeater
                  array(
                    // field in nested repeater
                    'field_5ae088b79d6fc' => "Test",
                  )
                )
              )
            );
    
          }
    
        $counter++;
    }
    update_field( $field_key, $data, $post_id );

    It’s still not adding the sub repeater fields with “Test”.

    I got closer trying this, it added the titles to 7 rows in the parent repeater field (which is correct) but then it added 7 child repeater rows to the 2nd, 3rd and 4th parent repeater with no data in them.

    It’s not quite right as I wanted it to add 7 rows of data to the child repeaters of each parent repeater with the data in them.

    Therefore building up the table in the image, with the column titles in a parent repeater field and the data in the columns as child repeater rows.

    // Product Code Titles
    $contents = $html->find('div[class=product-table]', 0);
    $data = array();
    $subdata = array();
    $rows = $contents->find('tr');
    $counter = 1;
    $field_key = "field_5ae0882f9d6f9";
    $sub_field_key = "field_5ae088999d6fb";
    
    foreach ($rows as $key_row => $row) {
    
        if($counter == 1) {
    
          foreach ($row->find('td') as $key_cell => $cell) {
    
            $data[] = array(
              "column_title" => strip_tags($cell->innertext),
            );
    
          }
    
        } elseif($counter >= 2) {
    
          foreach ($row->find('td') as $key_cell => $cell) {
    
            $subdata[] = array(
              "text" => strip_tags($cell->innertext),
            );
            add_sub_row( array($field_key, $counter, $sub_field_key), $subdata, $post_id );
          }
          
        }
    
        $counter++;
    }
    update_field( $field_key, $data, $post_id );