Forum


Replies: 1   Views: 154
How to add table inside 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 fairshareitservices  · 14-10-2024 - 05:22

I have a requirement to add one table in one row of the outer table, the next table in the second row, and so on. Now the no of rows of the outside table and inside table are dynamic but the columns are fixed. The outside table has one column and the inside table has 8 columns. So how to achieve this using a template and class CreateDocxFromTemplate

Posted by admin  · 14-10-2024 - 06:02

Hello,

You can use WordFragments to add content to tables (such as texts with styles, images, HTML, links,  other tables, charts...). You can add WordFragments in new contents, for example using addTable:

$valuesTable = array(
    array(
        'Title A',
        'Title B',
        'Title C',
    ),
    array(
        'Line A',
        $wordFragment1,
        $wordFragment2,
    )
);

$docx->addTable($valuesTable);

and replacing placeholders in templates, for example using replaceTableVariable:

$data = array(
                array(
                    'ITEM' => $wordFragment1,
                    'REFERENCE' => $wordFragment2,
                    'PRICE' => '5.45'
                ),
                array(
                    'ITEM' => $wordFragment3,
                    'REFERENCE' => $wordFragment4,
                    'PRICE' => '30.12'
                ),
                array(
                    'ITEM' => $wordFragment5,
                    'REFERENCE' => $wordFragment6,
                    'PRICE' => '7.00'
                )
        );

$docx->replaceTableVariable($data);

We recommend you check the samples included in the package, which illustrate adding WordFragments to tables using addTable, replaceTableVariable, and many other methods.

On https://www.phpdocx.com/documentation/practical/wordfragments-and-wordml you can read more information about working with WordFragments with phpdocx.

Regards.