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

标签 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     |
-----------------------------------------

在word模板中,我将这些行定义如下:
{name}
{address}
{lines}
{quantity}
{product_name}
{/lines}

当我执行脚本时,我在word文件中得到以下数据:
Name
Address 123
{!404_DOCXTemplate_quantity}
{!404_DOCXTemplate_product_name}

有人知道为什么我的代码的多行部分不能工作吗?
这是我的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');

}

以下函数生成文本!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;
}

最佳答案

你在while循环中犯了错

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

您使用$result2['quantity']而不是$row['quantity']和$result2['product_name']而不是$row['product_name']

关于php - 生成具有多行的Word文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54379059/

相关文章:

php - 深度加入Cakephp

JPA NativeQuery 中的 MYSQL 嵌套查询

php - 需要帮助来比较 PHP 和 DB 的值(value),$_POST 不起作用..?

javascript - '意外的标记 ;'当尝试在 jQuery AJAX 请求中传递 PHP 变量时

javascript - 使用 codeigniter 从下拉列表中填充文本框

php - 联系表未发送

mysql - 如何使用mysql搜索数组中的值?

javascript - 如何使用 while 循环为显示的每个输入按钮创建单独的单击事件

php - mysqli_fetch_assoc()需要参数/调用成员函数bind_param()错误。如何获取并修复实际的mysql错误?

PHP 错误 : mysql_num_rows(): supplied argument is not a valid MySQL