Home › Forums › Front-end Issues › 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:
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?
The topic ‘update_sub_field not inserting data in correctly’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.