Forum


Replies: 7   Views: 219
Create a while loop in php to create a 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 Expertise  · 15-05-2019 - 08:35

Goodmorning everyone.

I should make a simple word file that contains a table with three fields and can give the user the possibility to download it on the fly.
The data must be extracted from a DB.
How can I use a while loop in php inside the arrays used to compose the various data tables?

Greetings and thanks for the excellent work you do.

Posted by admin  · 15-05-2019 - 11:44

Hello,

We are not sure if we understand your question correctly. If you just need to create tables from an array you can just any loop function: for, while, foreach... ; phpdocx allows using any data source (databases, web services, strings, CSV...)

In the package you can find samples about how to generate a new table, for example:

// get the data for the database using PDO functions, an ORM or any other code
$data = ...;

// iterate the previous data to generate and add each new table
$i = 0;
while ($i < count($data)) {
  $valuesTable = $data[$i]; // an array structure
  $paramsTable = array(
      'border' => 'single',
      'tableAlign' => 'center',
      'borderWidth' => 10,
      'borderColor' => 'B70000',
      'tableWidth' => array('type' =>'pct', 'value' => 0),
  ); // add the needed table properties
  $docx->addTable($valuesTable, $paramsTable);
  $i++;
}

We recommend you to check and run the included samples and the documentation available on addTable.

Regards.

Posted by Expertise  · 16-05-2019 - 07:48

Hello, thanks for the reply and sorry for my bad English.

In fact, I didn't explain very well, but I was a bit of a hurry ...

How can I get a word file (.docx), which contains a table with a list of users extracted from a Mysql database (with a while loop)
I also wanted to know if it is possible to use a template for this.

Thank you
Greetings

Posted by admin  · 16-05-2019 - 08:44

Hello,

Sorry but we don't understand your question:

How can I get a word file (.docx), which contains a table with a list of users extracted from a Mysql database (with a while loop)<br /> I also wanted to know if it is possible to use a template for this.

Do you need to get the data of a table from a DOCX? Or do you need to add a table into a DOCX template?

If you need to get information from a DOCX you can use the following method: getWordContents

If you need to create and insert a table into a DOCX template you need to use a WordFragment:

https://www.phpdocx.com/documentation/practical/wordfragments-and-wordml

https://www.phpdocx.com/api-documentation/templates/replace-variable-word-fragment-Word-document

Regards.

Posted by Expertise  · 16-05-2019 - 10:04

Hi, thanks for the reply.

Said very simply:
- generate a file in .docx format
- inside the file there must be a table
- the table must have lines containing the last name, date of birth, country
- the data with which to populate the table must be taken from the db
- if in the db there are 20 lines, in the table of the generated word file there must be 20 lines
- I'd like to be able to use a template

It can be done and in the case of a positive response, how can I do it?

Best regards, thanks

Posted by admin  · 16-05-2019 - 10:18

Hello,

To accomplish that task using a template you can:

$docx = new CreateDocxFromTemplate('template.docx');

$data = array(
    array(
        'ITEM' => 'Product A',
        'REFERENCE' => '107AW3',
    ),
    array(
        'ITEM' => 'Product B',
        'REFERENCE' => '204RS67O',
    ),
    array(
        'ITEM' => 'Product C',
        'REFERENCE' => '25GTR56',
    ),
);
$docx->replaceTableVariable($data);

How to extract the information and generate an array from it depends on the function or method you are using to access the database: mysql functions, PDO, ORM...

We recommend you to check the included samples and the information available on the API pages of each method, that illustrate how to accomplish these kind of tasks.

Regards.

Posted by Expertise  · 17-05-2019 - 08:43

Hello again, many thanks for trying to help but I really can't.
I lock myself on the part where I need to generate rows dynamically from php.

I think that at this point I will opt for the purchase of Ticket so that I can solve the problem with your help.

To this end, I have version 7.5, does the ticker service also help with this version or does it only apply to the new one?

Kind regards

Posted by admin  · 17-05-2019 - 09:35

Hello,

Support tickets can be used with any license. Although we recommend using the latest release to be able to use all features and improvements (https://www.phpdocx.com/news/).

If you open a ticket and need support about getting information from the database please post in the ticket which database are you using and how are you accessing it: PDO, ORM, PHP functions...

Regards.