skip to Main Content

I’m trying to export a template.docx file in my PHP project using OpenTBS.

This is the source in my .php file (of course the array would be generated dynamically from a query, this is just a dummy):

$surname_list = array(
    'smith' => array(
        array(
            'n_name' => 'jhon',
            'n_surname' => 'smith',
        ),
        array(
            'n_name' => 'mark',
            'n_surname' => 'smith',
        ),
    ),
    'cole' => array(
        array(
            'n_name' => 'jenny',
            'n_surname' => 'cole',
        ),
        array(
            'n_name' => 'dennis',
            'n_surname' => 'cole',
        ),
        array(
            'n_name' => 'paul',
            'n_surname' => 'cole',
        ),
    ),
);

$TBS->MergeBlock('table_block', $surname_list);

And that’s what I have in my template.docx:

[table_block;block=begin]
[block.tbs:table]
Name       | Surname
[n_name]   | [n_surname]
[block.tbs:table;block=end]
[table_block;block=end]

Is even possible to achieve something like this?

Name Surname
jhon smith
mark smith
Name Surname
jenny cole
dennis cole
paul cole

In the documentation I didn’t find something useful for my purpose.

This is the only thing I archived in my template.docx, it does indeed iterate the "main" array, but nothing more:

[block.tbs:table]
Name       | Surname
[n_name]   | [n_surname]
[block.tbs:table;block=end]

[block.tbs:table]
Name       | Surname
[n_name]   | [n_surname]
[block.tbs:table;block=end]

UPDATE
by changing the array this way:

$surname_list = array(
        array(
            'surname' => 'smith',
            'people' => array(
                array(
                    'n_name' => 'jhon',
                    'n_surname' => 'smith',
                ),
                array(
                    'n_name' => 'mark',
                    'n_surname' => 'smith',
                ),
            ),
        ),
        array(
            'surname' => 'cole',
            'people' => array(
                array(
                    'n_name' => 'jenny',
                    'n_surname' => 'cole',
                ),
                array(
                    'n_name' => 'dennis',
                    'n_surname' => 'cole',
                ),
                array(
                    'n_name' => 'paul',
                    'n_surname' => 'cole',
                ),
            ),
        ),
    );

    $TBS->MergeBlock('table_block', $surname_list);

and the template.docx:

[table_block;block=begin]
[table_block.surname]
[table_block;block=end]

I managed to print:

smith
cole

Still no success on printing the "people".

2

Answers


  1. Chosen as BEST ANSWER

    I managed to solve the problem by editing the array structure this way:

    PHP

    $data = array(
        array(
            'surname' => 'Joe',
            'people' => array('Mirko','Monia')
        ),
        array(
            'surname' => 'Smith',
            'people' => array('Will')
        ),
        array(
            'surname' => 'Rossi',
            'people' => array('Mario','Luca','Paolo')
        ),
    );
    $TBS->MergeBlock('body', $data);
    

    template.docx

    template.docx

    Result

    result

    For further infos: Automatic subblocks

    Keep in mind that block=tr won't work if you try to export a .docx, block=tbs:row is to be used!


  2. There are several ways of doing repeated tables with TBS.
    You can use Sub-blocks or Grouping with parent, depending the structure of your data.

    See also the doc for Sub-blocks, and Groupings.

    Example 1 : (simplest) with parameter headergrp

    $surname_list_1 = array(
        array(
            'n_name' => 'jhon',
            'n_surname' => 'smith',
        ),
        array(
            'n_name' => 'mark',
            'n_surname' => 'smith',
        ),
        array(
            'n_name' => 'jenny',
            'n_surname' => 'cole',
        ),
        array(
            'n_name' => 'dennis',
            'n_surname' => 'cole',
        ),
        array(
            'n_name' => 'paul',
            'n_surname' => 'cole',
        ),
    );
    
    $TBS->MergeBlock('b1', $surname_list_1);
    

    template :

    [b1.n_surname] [b1;block=tbs:p+tbs:table;parentgrp=n_surname] (line on 1 paragraph)
    | Name                      | Surname        |
    | [b1.n_name;block=tbs:row] | [b1.n_surname] |
    

    Example 2 : with automatic-sub blocks

    $surname_list_2 = array(
        array(
            'surname' => 'smith',
            'people' => array(
                array(
                    'n_name' => 'jhon',
                    'n_surname' => 'smith',
                ),
                array(
                    'n_name' => 'mark',
                    'n_surname' => 'smith',
                ),
            ),
        ),
        array(
            'surname' => 'cole',
            'people' => array(
                array(
                    'n_name' => 'jenny',
                    'n_surname' => 'cole',
                ),
                array(
                    'n_name' => 'dennis',
                    'n_surname' => 'cole',
                ),
                array(
                    'n_name' => 'paul',
                    'n_surname' => 'cole',
                ),
            ),
        ),
    );
    
    $TBS->MergeBlock('b2', $surname_list_2);
    

    template :

    [b2.surname] [b2;block=tbs:p+tbs:table;sub1=people] (line on 1 paragraph)
    | Name                           | Surname             |
    | [b2_sub1.n_name;block=tbs:row] | [b2_sub1.n_surname] |
    

    Example 3 : (more complicated) sub-block with dynamic query

    $surname_list_3 = array(
        'smith' => array(
            array(
                'n_name' => 'jhon',
                'n_surname' => 'smith',
            ),
            array(
                'n_name' => 'mark',
                'n_surname' => 'smith',
            ),
        ),
        'cole' => array(
            array(
                'n_name' => 'jenny',
                'n_surname' => 'cole',
            ),
            array(
                'n_name' => 'dennis',
                'n_surname' => 'cole',
            ),
            array(
                'n_name' => 'paul',
                'n_surname' => 'cole',
            ),
        ),
    );
    
    $TBS->MergeBlock('b3M', $surname_list_3);
    
    $TBS->SetVarRefItem('surname_list_3', $surname_list_3);
    $TBS->MergeBlock('b3S', 'array', 'surname_list_3[%p1%]');
    

    template :

    [b3M.$] [b3M;block=tbs:p+tbs:table] (line on 1 paragraph)
    | Name                                  | Surname        |
    | [b3S.n_name;block=tbs:row;p1=[b3M.$]] | [b3S.n_surname] |
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search