Support

Account

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

Solving

Update parent and child repeater programmatically

  • I’m looping through the following table:

    Table

    Grabbing the titles and inputting that into a parent repeater. I’m then trying to loop through the rest of the table and enter those into the child repeater.

    At the moment it enters the titles into the parent repeater but no rows are being created for the child repeater with no values input.

    Where am I going wrong?

        // Product Code Titles
        $contents = $html->find('div[class=product-table]', 0);
        $data = array();
        $subdata = array();
        $rows = $contents->find('tr');
        $counter = 1;
        foreach ($rows as $key_row => $row) {
        
            if($counter == 1) {
        
              foreach ($row->find('td') as $key_cell => $cell) {
        
                $field_key = "field_5ae0882f9d6f9";
                $data[] = array(
                  "column_title" => strip_tags($cell->innertext),
                );
        
              }
        
            } elseif($counter >= 2) {
        
              foreach ($row->find('td') as $key_cell => $cell) {
        
                $sub_field_key = "field_5ae088999d6fb";
                $subdata[] = array(
                  "text" => strip_tags($cell->innertext),
                );
        
                update_sub_field( $sub_field_key, $subdata, $post_id );
        
              }
        
            }
        
            $counter++;
        }
        update_field( $field_key, $data, $post_id );
  • From what I can see you are updating a subfield for a repeater that does not exist yet and then you’r updating the repeater with empty information except for the title.

    When updating a repeater field the value $data should be a multidimensional array.

    
    $data = array(
      // array element for each row
      array(
        // an element for each field
        // since field does not exist always use field keys
        'field_123345' => 'value for field 1',
        'field_234567' => 'value for field 2',
        'field_456789' => 'value for field 3',
        // etc...
      ),
      array(
        // an element for each field
        // since field does not exist always use field keys
        'field_123345' => 'value for field 1',
        'field_234567' => 'value for field 2',
        'field_456789' => 'value for field 3',
        // etc...
      )
    );
    
    update_field('field_key_for_repeater', $value, $post_id);
    
  • Hi John, thanks for replying. Will that update both the parent and child repeaters? The first if statement works, it’s adding the first row of the table as 7 repeater rows with the column titles.

    It’s more the child repeater where I want to add the data in each column to the child repeater.

  • if you have a nested repeater you can nest that in the array

    
    $value = array(
      // element for row
      array(
        // field in parent repeater
        'field_12345' => 'value'
        
        // nested repeater
        'field_23456' => array(
          // element for row in nested repeater
          array(
            // field in nested repeater
            'field_34567' => 'value for field in nested repeater'
          )
        )
      )
    );
    
  • 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 );
  • It has to be in the way that you’re constructing the nested array. Going to be honest, I’m not exactly sure what the problem is. In your first code if you can output the array that you’ve constructed that might tell you where it’s going wrong

    
    echo '<pre>'; print_r($data); echo '</pre>';
    update_field( $field_key, $data, $post_id );
    

    I can tell you that in your second code you have a similar problem to the first code you poasted. You are trying to add a sub row to a repeater that does not exist yet. add_sub_row() is called before the update_field() call that creates the repeater that you want to add the row to.

  • I’ve moved

    update_field( $field_key, $data, $post_id );
    update_sub_field( array($field_key, 1, $sub_field_key), $subdata, $post_id );

    Underneath:

    update_field

    I just set the count to 1 so it’s added ALL cell data except from the first row to the child repeater field of the first parent repeater row. Slight progress!!

    I now need to add the correct data to the $subdata variable.

  • Hi John,

    I’ve progressed this slightly.

    It’s iterating through and adding the column titles to 7 repeater rows. So that works.

    The update_sub_repeater is adding data to all 7 child repeater fields but it’s counting up for some reason… it’s adding 2 child repeater rows to the first parent repeater, 3 to the second, 4 to the third and so on.

    It’s so close apart from this final part, where am I going wrong?

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

The topic ‘Update parent and child repeater programmatically’ is closed to new replies.