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++;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证