【问题标题】:Replacing content of block after generating clones using cloneBlock - PHPWord使用 cloneBlock 生成克隆后替换块的内容 - PHPWord
【发布时间】:2019-05-23 19:16:11
【问题描述】:

请谁能告诉我在 phpword 中使用 cloneblock 的语法。

所以我已经在 MySQL 数据库中获得了数据,对于我需要通过 phpword 导入到我的 word doc 中的单行,它工作正常......运行我的查询和搜索并用模板处理器替换。但是,现在我想在我的 word 文档中插入多行。我研究并发现 cloneblock 方法就是答案。但是我无法让它工作......目前我的代码运行但它似乎没有到达第二行。

我实际上没有收到任何错误消息。我的代码执行得很好……但是最后显示的 word 文件没有显示得很好……如果你看到我的代码,我得到了一个回显语句……哪个回显在我的浏览器中正是我想要的“损坏”& “好”,(作为其中一个行数据的示例),但该数据不会像那样被拉入我的 word 文档中......它重复“损坏”,“损坏”。 .

$group_key=1;

do {    

  //loop to increase my uuid  - ($repeatgroup')
  $repeatgroup = $id."/"."trailer_repeat_group"."[".$group_key."]";

// query string
  $trailer_repeat_grouping = mysqli_query($connect, "SELECT * FROM trailer_repeat_group LEFT JOIN main on trailer_repeat_group.PARENT_KEY = main.metainstanceID WHERE trailer_repeat_group.KEY_id = '$repeatgroup'");

  $templateProcessor->cloneBlock('CLONEME', $trailer_count);

  while ($row1 = mysqli_fetch_array($trailer_repeat_grouping)) {    

    //this echo below I am using to test exactly what happends – independent of 
    //PHPword/templateprocessor
    echo $rttc =  $row1['right_trailer_tyre_condition'];

    //inserting  / searching / inserting values
    $templateProcessor->setValue("right_trailer_tyre_condition", $rttc);
  }

  // ending of loop / checking loop

  $group_key++;

} while ($group_key <= $trailer_count);

【问题讨论】:

  • 专业提示:如果您向我们展示您正在使用的实际代码,您总会得到社区更好的回应
  • 示例在 repo 的 Samples 文件夹中:github.com/PHPOffice/PHPWord/blob/develop/samples 和 cloneBlock 示例在这里:github.com/PHPOffice/PHPWord/blob/develop/samples/… 在每条语句上方都有 cmets 记录
  • 你读过这个existing Q&A
  • 请将您的代码放入您的问题中,该问题不起作用
  • $group_key=1;做 { $repeatgroup = $id."/"."trailer_repeat_group"."[".$group_key."]"; $trailer_repeat_grouping = mysqli_query($connect, "SELECT * FROM trailer_repeat_group LEFT JOIN main on trailer_repeat_group.PARENT_KEY = main.metainstanceID WHERE trail_repeat_group.KEY_id = '$repeatgroup'"); $templateProcessor->cloneBlock('CLONEME', mysqli_num_rows($trailer_repeat_grouping)); $templateProcessor>setValue("right_trailer_tyre_condition",$row['Left_tyre_condition'], 1); $group_key++; } while ($group_key

标签: php mysql phpword


【解决方案1】:

我已经调查过了,找到了解决办法。

您正在克隆相同的块N次

$templateProcessor->cloneBlock('CLONEME', $trailer_count);

然后通过 fetch 您尝试将 right_trailer_tyre_condition 替换为某个值:

$templateProcessor->setValue("right_trailer_tyre_condition", $rttc);

问题是您要替换 所有占位符

但实际上您需要将它们一个替换为一个不同的值。

解决方案是定义第三个参数,表示要替换的项目数

只需将其更改为:

$templateProcessor->setValue("right_trailer_tyre_condition", $rttc, 1);

【讨论】:

  • 嗨..我已经尝试过了...但是当添加 1 时,它只会填充第一个克隆块而不是第二个...,如果我将其更改为 2,那么它会填充第二个也是。非常感谢您向我指出这一点。你确实有帮助。但是,我正在提出一个新问题。我对图像有同样的问题。它通过克隆块复制它。
猜你喜欢
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-17
  • 1970-01-01
  • 2015-07-04
相关资源
最近更新 更多