phpLiveDocx allows designers to insert any number of text fields into a template. These text fields are populated with data when createDocument() is called.
In addition to text fields, it is also possible specify regions of a document, which should be repeated.
For example, in a telephone bill it is necessary to print out a list of all connections, including the destination number, duration and cost of each call. This repeating row functionality can be achieved with so called blocks.
Blocks are simply regions of a document, which are repeated when createDocument() is called. In a block any number of block fields can be specified.
Blocks consist of two consecutive document targets with a unique name. The following screenshot illustrates these targets and their names in red:

The format of a block is as follows:
blockStart_ + unique name
blockEnd_ + unique name
For example:
blockStart_block1
blockEnd_block1
The content of a block is repeated, until all data assigned in the block fields has been injected into the template.
Consider the following PHP5 code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $mailMerge = new Zend_Service_LiveDocx_MailMerge(); $mailMerge->setUsername('myUsername') ->setPassword('myPassword'); $mailMerge->setLocalTemplate('template.doc'); $billConnections = array ( array ('connection_number' => '+49 421 335 912', 'connection_duration' => '00:00:07', 'fee' => '€ 0.03'), array ('connection_number' => '+49 421 335 913', 'connection_duration' => '00:00:07', 'fee' => '€ 0.03'), array ('connection_number' => '+49 421 335 914', 'connection_duration' => '00:00:07', 'fee' => '€ 0.03'), array ('connection_number' => '+49 421 335 916', 'connection_duration' => '00:00:07', 'fee' => '€ 0.03') ); $mailMerge->assign('connection', $billConnections); $mailMerge->createDocument(); |
The data, which is specified in the array $billConnections is repeated in the template in the block connection. The keys of the array (connection_number, connection_duration and fee are the block field names – their data is inserted, one row per iteration.
You can download the DOC template file here:
- template.doc [20.5 KB]
And the resulting PDF document here:
- document.pdf [77.6 KB]
Note that blocks may not be nested.
To try out this example for yourself, download phpLiveDocx. It is contained in the directory /samples/mail-merge/telephone-bill/
March 2nd, 2009 at 06:49
How do you add a ‘block’ to a word document?
March 7th, 2009 at 13:45
This procedure is described in the blog post above. May I ask, with which part are you having difficulties. Thank you.
March 12th, 2009 at 19:26
I think he is asking about how do you insert and find/see the “blockstart_connection”/end in Word, because the fields are clear but the block start and end can not be seen in the doc file.
March 20th, 2009 at 15:11
Hi,
Is it possible to mail merge image using phplivedocx? Can you show an example?
Thanks.
Florante
March 20th, 2009 at 15:51
Hello Florante
Currently, this is not possible. However, it is by far one of the most frequently requested features and will be implemented in a future version.
For details, please take a look at this thread in the ‘feature request’ forum:
http://www.theimagingsourceforums.com/showthread.php?t=321766
Jonathan Maron
June 17th, 2009 at 06:24
Oxygen,
I couldn’t work out that either! But it appears they aren’t visible as they are bookmarks. You can add the blockstart_connection/end in Word by adding the bookmarks like so:
1 – Select where you want the bookmark to go
2 – Insert > Bookmark > Type name (blockstart_connection) and click ‘Add’
3 – Repeat for end bookmark
Job done :)
Cheers,
James
September 11th, 2012 at 15:15
Really, Jonathan Maron? How to add ‘blocks’ (actually, bookmarks) to a word document is outlined in the blog post? Where? Thank you james cooke, for actually making a helpful post.