首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >循环上复制自身的迭代

循环上复制自身的迭代
EN

Stack Overflow用户
提问于 2018-06-28 08:57:39
回答 1查看 69关注 0票数 0

我使用字段函数将循环中的内容添加到子中继器字段中。

我已经到了数据被添加到子中继器的地步,但它正在覆盖自己。它只更新子中继器中的第一行,所以我只看到最后一个循环。

我使用$counter迭代父中继器,但是当我试图向子中继器添加迭代时,它会中断函数。就像这样:

代码语言:javascript
运行
复制
update_sub_field( array($field_key, $counter, $sub_field_key, $rowcount), $value, $post_id );

我试过另一个函数add_sub_row..。这会将正确的行数添加到子中继器中,但不会添加数据。

这是我的完整代码:

代码语言:javascript
运行
复制
// 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++;

}

为了给出一些上下文,我正在重新创建这个表,将单元格数据放入子中继器字段中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-28 12:21:09

在这个问题上没有太多的内容,所以希望这能帮助到别人。

我停止了它对$value数组的附加,将函数更改为update_sub_row,并添加了一个计数器来迭代子中继器行。

下面是完整的代码:

代码语言:javascript
运行
复制
// 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++;

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51078678

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档