生成具有多行的Word文件时出错

Joh*_*ohn 8 php mysql mysqli ms-word

我试图从我的数据库中选择数据并在Word(DOCX)文件中显示数据.

我正在使用两种类型的数据.

第一种类型是我想要显示一次的数据.喜欢客户名称.

第二种类型的数据是使用动态行脚本导入的数据.我的数据库中的数据如下所示:行:

-----------------------------------------
| internal_id | quantity | product_name |
|      1      |     1    |     One      |
|      1      |     5    |     Two      |
|      1      |     4    |    Three     |
|      1      |     2    |     Four     |
|      1      |     6    |     Five     |
-----------------------------------------
Run Code Online (Sandbox Code Playgroud)

在我的Word模板中,我将这些行定义如下:

{name}
{address}
{lines}
{quantity}
{product_name}
{/lines}
Run Code Online (Sandbox Code Playgroud)

当我执行我的脚本时,我在我的Word文件中获得以下数据:

Name
Address 123
{!404_DOCXTemplate_quantity}
{!404_DOCXTemplate_product_name}
Run Code Online (Sandbox Code Playgroud)

有人知道为什么我的代码的多行部分不起作用?

这是我的PHP脚本,我选择数据并生成Word文件:

$result1 = mysqli_fetch_assoc($res1) ;
$link2 = mysqli_connect("localhost", "root", "pass", "db");

if($link2 === false){
  die("ERROR: Could not connect. " . mysqli_connect_error());
}

$sql2 = "SELECT * FROM lines WHERE internal_id = '1'";
$res2 = mysqli_query($link2, $sql2) ;

$result2 = $link2->query($sql2);
if ($result2->num_rows > 0) {
$result2 = mysqli_fetch_assoc($res2) ;

  include_once('./docxtemplate.class.php');

  $docx = new DOCXTemplate('template.docx');

  $docx->set('name', "" . $result1['name'] . "");
  $docx->set('address', "" . $result1['address'] . "");

  $docx->set('LINES', mysqli_num_rows($result2));
  while ($row = mysql_fetch_array($result2))
    {
      $docx->set('quantity', "" . $result2['quantity'] . "");
      $docx->set('product_name', "" . $result2['product_name'] . "");
    }
  $docx->saveAs('test.docx');

  header("Content-Type:application/msword");
  header("Content-Disposition: attachment;filename=test.docx");

  readfile('test.docx');

}
Run Code Online (Sandbox Code Playgroud)

以下函数生成文本!404_DOCXTemplate_ docxtemplate.class.php

private function _parse() {
    if ($doc = $this->getEntryData( 'word/document.xml' )) {
        if (preg_match_all('/\{[^}]+\}/', $doc, $m )) {
            foreach( $m[0] as $v ) {
                $var = preg_replace('/[\s\{\}]/','', strip_tags( $v ));
                if (isset( $this->data[ $var ] )) {
                    if (is_scalar( $this->data[ $var ] ))
                        $doc = str_replace( $v, $this->_esc( $this->data[ $var ] ), $doc );
                } else {
                    $doc = str_replace( $v, '{!404_'.__CLASS__.'_'.$var.'}', $doc );
                }
            }
        }
        $this->setEntryData( 'word/document.xml', $doc );
        return true;
    } else 
        return false;
}
Run Code Online (Sandbox Code Playgroud)