Support

Account

Home Forums Front-end Issues update_sub_field not inserting data in correctly

Unread

update_sub_field not inserting data in correctly

  • I have a loop that is inserting column titles from a table into a parent repeater, then cell data into a child repeater. Essentially this:

    Table

    The titles work fine. The child repeater doesn’t work as expected though.

    Here’s the 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
    // $key = key($rows); // unused
    $row = current($rows);
    foreach ($row->find('td') as $cell) {
        $titles[] = array("column_title" => strip_tags($cell->innertext));
    }
    
    // here we continue iteration starting from second row
    $rowsCount = 0;
    while($row = next($rows)){
        $rowsCount++;
    
        $subdata = array();
        foreach ($row->find('td') as $cell) {
            $subdata[] = array("text" => strip_tags($cell->innertext));
        }
    
        echo '<pre>'; print_r($subdata); echo '</pre>';
        update_sub_field( array($field_key, $rowsCount, $sub_field_key), $subdata, $post_id );
    
    }
    update_field( $field_key, $titles, $post_id );

    Where I’m showing the $subdata array, it’s outputting the following:

    Array
    (
        [0] => Array
            (
                [text] => 038-94-830
            )
    
        [1] => Array
            (
                [text] => LarySeal Pro Size 3
            )
    
        [2] => Array
            (
                [text] => 30Kg - 50Kg
            )
    
        [3] => Array
            (
                [text] => 3
            )
    
        [4] => Array
            (
                [text] => 7.5
            )
    
        [5] => Array
            (
                [text] => 16
            )
    
        [6] => Array
            (
                [text] => 10
            )
    
    )
    Array
    (
        [0] => Array
            (
                [text] => 038-94-840 
            )
    
        [1] => Array
            (
                [text] => LarySeal Pro Size 4
            )
    
        [2] => Array
            (
                [text] => 50Kg - 70Kg 
            )
    
        [3] => Array
            (
                [text] => 4
            )
    
        [4] => Array
            (
                [text] => 8
            )
    
        [5] => Array
            (
                [text] => 18
            )
    
        [6] => Array
            (
                [text] => 10
            )
    
    )
    Array
    (
        [0] => Array
            (
                [text] => 038-94-850
            )
    
        [1] => Array
            (
                [text] => LarySeal Pro Size 5
            )
    
        [2] => Array
            (
                [text] => 70Kg - 100Kg
            )
    
        [3] => Array
            (
                [text] => 5
            )
    
        [4] => Array
            (
                [text] => 8
            )
    
        [5] => Array
            (
                [text] => 18
            )
    
        [6] => Array
            (
                [text] => 10
            )
    
    )

    This is correct but when I use the update_sub_field function it only takes the last row of the table (it looks as though it’s copied over itself).

    Where am I going wrong with the function because the array of data is correct?

Viewing 1 post (of 1 total)

The topic ‘update_sub_field not inserting data in correctly’ is closed to new replies.