#StackBounty: #php #mysql #mysqli #ms-word Error when generating Word file with multi rows

Bounty: 100

I am trying to SELECT data from my database and show the data in a Word (DOCX) file.

I am working with two type of data.

The first type is data that I want to show once. Like the customer name.

The second type of data is data that is imported with a dynamic row script. The data in my database looks like:
Lines:

-----------------------------------------
| internal_id | quantity | product_name |
|      1      |     1    |     One      |
|      1      |     5    |     Two      |
|      1      |     4    |    Three     |
|      1      |     2    |     Four     |
|      1      |     6    |     Five     |
-----------------------------------------

In my Word template I defined these rows as follow:

{name}
{address}
{lines}
{quantity}
{product_name}
{/lines}

When I execute my script I get the following data in my Word file:

Name
Address 123
{!404_DOCXTemplate_quantity}
{!404_DOCXTemplate_product_name}

Does someone know why the multi row part of my code is not working?

Here is my PHP script where I am selecting the data and generate the Word file:

$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');

}

The following function is generating the text !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;
}


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.