Support

Account

Home Forums Front-end Issues Iterate update_sub_field

Helping

Iterate update_sub_field

  • I’m using update_sub_field to update a child repeater. After many attempts this is the closest I’ve got.

    At the moment it just adds the data from the last loop through so I think it’s just overwriting the first row of the child repeater on each loop through.

    Is it possible to do something like the following:

    update_sub_field( array($field_key, $counter, $sub_field_key, $rowcount), $value, $post_id );

    With $counter being the parent row and $rowcount being the child repeater row?

    Here’s the full code:

    // Product Code Titles
    $rows = $html->find('div[class=product-table]', 0)->find('tr');
    $field_key = "field_5ae0882f9d6f9";
    $sub_field_key = "field_5ae088999d6fb";
    
    if(empty($rows)){
        die("Empty array");
    }
    
    $titles = array(); // aka your $data
    
    // here we fetch the first row and iterate to get titles
    $row = current($rows);
    foreach ($row->find('td') as $cell) {
        $titles[] = array("column_title" => strip_tags($cell->innertext));
    }
    update_field( $field_key, $titles, $post_id );
    
    // here we continue iteration starting from second row
    $value = array();
    $rowcount = 1;
    while($row = next($rows)){
    
        $cells = $row->find('td');
        $columnsCount = count($cells);
        $counter = 1;
        foreach ($cells as $cell) {
          $value[] = array("text" => strip_tags($cell->innertext));
          update_sub_field( array($field_key, $counter, $sub_field_key), $value, $post_id );
          echo '<pre>'; print_r($value); echo '</pre>';
          $value = array();
          $counter++;
        }
    
      $rowcount++;
    
    }
Viewing 1 post (of 1 total)

The topic ‘Iterate update_sub_field’ is closed to new replies.