Forum


Replies: 12   Views: 4785
Addtemplatevariable only creates first row of table
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by lbreu  · 19-01-2012 - 16:41

Hi

I am still trying to figure out this table thing, which is the main reason we purchased phpdocx pro.

I created the simplest template I could think of using Word 2007. It's just a table with the first row containing text (header) and then one row with variables, just like in your demo.

I even enclosed it in $BLOCK_table$, just like the demo. Also there is a variable $vardump$ after that, and that's it.

I then created an array of arrays (3 data rows) and used
$docx->addTemplateVariable($terminData, 'table', array('header' => false));
to create the table. I also replaced the vardump variable with a var_dump of my data array.

The resulting docx has only one row in the table (plus the fixed header), but the vardump shows that there are actually three items in the array (each of which is itself an array of course).

What could be the problem?
Things I checked:
- special chars like Umlauts --> not the reason, there are none in my testdata
- empty variables --> not the reason, if I prepend "foo" to all values, it still only creates one row

What am I doing wrong? I really need this to work, but I seem to be missing something here. I would gladly send you the template and the resulting docx as well as some more code, if you could give me an idea on how to fix this...

Cheers,
Lorenz

Posted by lbreu  · 11-04-2013 - 12:13

OK, yet another self reply ;)

Did some more tests, it looks like the autocorrect may be messing things up. I guess it's open source and I could fix it, but since I am paying for it, I hope you guys may be able to address this in a patch at some point ;) Or maybe I am just doing it wrong???

Here a snippet from OpenOffice which works:

[code]
<w:tr>
<w:trPr>
<w:cantSplit w:val="false"/>
</w:trPr>
<w:tc>
<w:tcPr>
<w:tcW w:type="dxa" w:w="4819"/>
<w:tcBorders>
<w:left w:color="000000" w:space="0" w:sz="2" w:val="single"/>
<w:bottom w:color="000000" w:space="0" w:sz="2" w:val="single"/>
</w:tcBorders>
<w:shd w:fill="auto" w:val="clear"/>
<w:tcMar>
<w:top w:type="dxa" w:w="55"/>
<w:left w:type="dxa" w:w="55"/>
<w:bottom w:type="dxa" w:w="55"/>
<w:right w:type="dxa" w:w="55"/>
</w:tcMar>
</w:tcPr>
<w:p>
<w:pPr>
<w:pStyle w:val="style20"/>
</w:pPr>
<w:r>
<w:rPr></w:rPr>
<w:t>$termin.datum$</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:type="dxa" w:w="4819"/>
<w:tcBorders>
<w:left w:color="000000" w:space="0" w:sz="2" w:val="single"/>
<w:bottom w:color="000000" w:space="0" w:sz="2" w:val="single"/>
<w:right w:color="000000" w:space="0" w:sz="2" w:val="single"/>
</w:tcBorders>
<w:shd w:fill="auto" w:val="clear"/>
<w:tcMar>
<w:top w:type="dxa" w:w="55"/>
<w:left w:type="dxa" w:w="55"/>
<w:bottom w:type="dxa" w:w="55"/>
<w:right w:type="dxa" w:w="55"/>
</w:tcMar>
</w:tcPr>
<w:p>
<w:pPr>
<w:pStyle w:val="style20"/>
</w:pPr>
<w:r>
<w:rPr></w:rPr>
<w:t>$termin.beschreibung$</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
[/code]


and here the same table row structure with an unmodified, untampered with, unsuperconfigured out-of-the-box Word 2007 which doesn't (only first row of data is output):


[code]
<w:tr w:rsidR="001F161E" w:rsidTr="001F161E">
<w:tc>
<w:tcPr>
<w:tcW w:w="4606" w:type="dxa"/>
</w:tcPr>
<w:p w:rsidR="001F161E" w:rsidRDefault="001F161E">
<w:r>
<w:t>$</w:t>
</w:r>
<w:proofErr w:type="spellStart"/>
<w:r>
<w:t>termin.datum</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
<w:r>
<w:t>$</w:t>
</w:r>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w="4606" w:type="dxa"/>
</w:tcPr>
<w:p w:rsidR="001F161E" w:rsidRDefault="001F161E">
<w:r>
<w:t>$</w:t>
</w:r>
<w:proofErr w:type="spellStart"/>
<w:r>
<w:t>termin.beschreibung</w:t>
</w:r>
<w:proofErr w:type="spellEnd"/>
<w:r>
<w:t>$</w:t>
</w:r>
</w:p>
</w:tc>
</w:tr>
[/code]

Posted by admin  · 11-04-2013 - 12:13

Hello,

We'll check it ASAP.

Regards.

Posted by pks  · 11-04-2013 - 12:13

There is a bug, and it's a very very strange one. I spent hours trying to figure out, and the only work-around that got me over this was "cleaning" the variable names. Meaning writing the variable name in a non-formatted editor, like notepad, then copying the plain text variable name into word. It seems that some formatting issues break template table parsing.

Really looking forward to an official response on this, it took me hours to reformat an entire report this way, and in the future it will be more work again when something changes + it's very hard to track since client won't know it the table is really 1 row or it's bug from phpdocx

Posted by pks  · 11-04-2013 - 12:13

And since there's already another hint from another user, the developers might want to check the underscore parsing. That could be the issue. Although with the method above (writing $VARIABLE_NAME$ into notepad then pasting into work) I did manage to have a working table with underscore variables. I'm not sure underscore is the issue, since this 1 row bug appeared for me for non-underscore variables too.

Posted by dlange87  · 11-04-2013 - 12:13

Same problem here ... and I can confirm that this only occurs when working directly in Word.
After your hint, I opened the file in OpenOffice, saved as XML, reopened in Word and saved directly docx (without making any changs). Thereafter everything worked as intended.

Would also need a fix for this ...

Posted by jc.021286  · 11-04-2013 - 12:13

Okay,
I've run into the same issue. Everything stopped working and I downloaded the source starting example and spent the last 12 hours trouble shooting this.

From the original
PHP Arrays are fine. Adjust the VAR_ITEM and VAR_PRICE values and everything populates fine.

In Word 2007.
Add more columns to the right and in the middle, add their respective variables names $VAR_TEST$, $VAR_TEST1$, ++ all work fine. Underscores and numbers seem to work just fine.

Added up to 8 columns, works fine.

Adjusted the table format, it carried forward almost perfectly. Table styles with alternating row colours do not take affect, the row after the header colour scheme will be applied throughout.

Adjust VAR_PRICE to anything else and it works fine.


Moment of truth, adjust VAR_ITEM to VAR_ITEMS... 1 row... *crap*
>>> Now for the fun part, remove the "S" with backspace so it now reads, VAR_ITEM.
>>>STILL DOES NOT WORK.... ???

Hit ctrl+z twice to make it go from VAR_ITEM > VAR_ITEMS > VAR_ITEM
>>>Test and it now works...


The gentleman who mentioned copy paste through notebook, you have my thanks.
As far as I noticed so far, only 1 column needs the correctly *notepaded* value to start the array off. The other columns follow suit without requiring to be *notepaded* (NP), (only tested with the leftmost starting column).
*** tested, at least 1 column variable needs to be NP.

Hope this helps some of you,
I do not have a lot of experience with xml, but I'll see if I cannot find it and see if a scrubber function is not viable.

jc

Posted by Merlin1  · 11-04-2013 - 12:13

I had the same problem with variables, the solution is to define them in microsoft word not just writing $variable$.
You can do this :

1. On the Insert menu, click Field.
2. Note In Microsoft Office Word 2007, click Quick Parts in the Text group on the Insert tab, and then click Field.
3. In the Categories box, select Document Automation.
4. In the Field names list, select DocVariable.
5. In the New Name box, under Field properties, type the name of the document variable.

I hope this helps :)

Posted by m.elfring  · 09-12-2013 - 13:23

Hello,



After hours and hours of debugging i discovered that the array containing the values of your variables requires the first element to be the first element in your table!



E.g. if your template table is like this:



Name | Adres

------------------------

$NAME$ | $ADDRESS$



Your array should look like this:

[0]=>array(

[NAME]=>my name

[ADDRESS]=>an address) 


[1]=>array(

[NAME]=>other name

[ADDRESS]=>other address) 



My array is filled by a query that is executed against a database and contained more fields than defined in the template table. This resulted in only one row getting filled. 



E.g. this did not work:



[0]=>array(

[COUNTRY]=>NL

[NAME]=>my name

[ADDRESS]=>an address) 

[1]=>array(

[COUNTRY]=>NL

[NAME]=>other name

[ADDRESS]=>other address) 



And this did work:

[0]=>array(

[NAME]=>my name

[ADDRESS]=>an address) 

[COUNTRY]=>NL

[1]=>array(

[NAME]=>other name

[ADDRESS]=>other address

[COUNTRY]=>NL)



Hope this helps.

 



 


 


Posted by PeerBr  · 13-01-2014 - 16:00

I was struggling with the same problem, but took a long hard look at PHPDocx's sourcecode and came up with what I did wrong:



The tutorial states $docx->addTemplateVariable($aData'table'array('header' => false));



But it also has a template block called "table", which is confusing. Because I want the theoretical ability to have n tables, and remove them if necessary, I enclosed them with $BLOCK_table1$, $BLOCK_table2$ etc., you get the deal. I assumed that I needed to do specify the block the variable should go to. That was wrong - "table" may be what the block is called, but "table" is also a fixed param. Anything value but "list" or "table" for that param will cause your array of variables to be evaluated only once, replacing what is inside the first row, but not duplicating it. Now I don't know if PHPDocx is able to have more than one table, it may be. If you want to try it, make sure the first variable in it is unique, as this is used to duplicate the rows. To be safe, all variables should be unique, though.



The maintainers of PHPDocx should perhaps update the documentation and change the block titles to "BLOCK_mytable".



Hope this helps somebody.



PS: These "notepad" tricks posted above are a bit misleading. Docx files are zipped folders containing XML, and when you have invalid variables, you need to open your .docx using not Word, but a program like WinRAR to see the files contained. Look for "word\document.xml". You can edit this file using any text editor (I prefer Altova XML Spy), and search for "$" if that is your variable delimiter. If you find something like "$frodoBag", then a bunch of XML, then "ginsOfTheShire$", you can safely delete anything in between until re-joining the variable "$frodoBagginsOfTheShire$" again. To be safe, you should delete auto-correction (both grammar and spelling) and tracking changes in Word. These are responsible for chopping up your text and insert invisible divisions. To be safer, either fully copy your variables from a plain-text file instead of typing them, or type them in one go. If you need to correct them, delete them as a whole and re-type them.


Posted by PeerBr  · 13-01-2014 - 16:04

I meant "disable auto-correction", not delete. And you need to save your edited files: WinRAR will ask you if you want to save your modified file back into the .docx and replace the original document.xml, which you want to do, otherwise you'll lose your edits.

Posted by jorgelj  · 13-01-2014 - 16:20

Hello,



PHPDocX should clean the unwanted tags between placeholders. Could you attach a document to check it?



Regards.