douliang2087 2018-06-28 08:57
浏览 44
已采纳

迭代在循环上复制自身

I'm using ACF's update_sub_field function to add content from a loop to a child repeater field.

I've got it to a point where the data is being added to the child repeater but it's overwriting itself. It only ever updates the first row in the child repeater so I only ever see the last loop through.

I'm iterating the parent repeater using $counter, but when I try to add an iteration to the child repeater it breaks the function. Like so:

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

I've tried another function add_sub_row... this adds the correct number of rows to the child repeater's but doesn't add the data.

Here's my 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++;

}

Just to give some context, I'm recreating this table with the cell data being put into a child repeater field.

enter image description here

  • 写回答

1条回答 默认 最新

  • dp20011 2018-06-28 12:21
    关注

    There's not much on this subject so hope this helps someone else.

    I stopped it appending to the $value array, changed the function to update_sub_row and added a counter to iterate the child repeater rows.

    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("field_5ae088b79d6fc" => strip_tags($cell->innertext));
          update_sub_row( array($field_key, $counter, $sub_field_key), $rowcount, $value, $post_id );
          echo '<pre>'; print_r($value); echo '</pre>';
          $value = array();
          $counter++;
        }
    
      $rowcount++;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭