1

I'am using a library called OpenTbs to create an odt with PHP, I use it because dynamically generates columns and rows.

I know how to create the rows and the columns, what I don't know is how to organize them.

let me add an example:

So first I will add this on my odt,

+-- ---------------------------------------------------+
|    Thin              |    Heavy   |      Total       |
+------------------------------------------------------+
| [b.date]             |            |                  |
+------------------------------------------------------+
| [b.thin;             |            |                  |
| block=tbs:cell;      |            |                  |
|  parallel=tbs:table] |            |                  |
|                      |            |                  |
+------------------------------------------------------+
| [b.heavy]            |            |                  |
+------------------------------------------------------+
| [b.total]            |            |                  |
+------------------------------------------------------+

and then in the code I will use:

<?php
include_once('tbs_class.php');
include_once('plugins/tbs_plugin_opentbs.php');

$TBS = new clsTinyButStrong;
$TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);

$TBS->LoadTemplate('template.odt',OPENTBS_ALREADY_UTF8);
$data = array(
    array('date' => '2013-10-13', 'thin' => 156, 'heavy' => 128, 'total' => 284),
    array('date' => '2013-10-14', 'thin' => 233, 'heavy' =>  25, 'total' => 284),
    array('date' => '2013-10-15', 'thin' => 110, 'heavy' => 412, 'total' => 130),
);
$TBS->MergeBlock('b', $data);



// $TBS->Plugin(OPENTBS_DEBUG_INFO, true); 

$output_file_name ="test_download.odt";
$TBS->Show(OPENTBS_DOWNLOAD, $output_file_name); 
?>

Output:

+ --------------------------------------+
|    Thin    |    Heavy   |   Total     |
+---------------------------------------+
| 156        | 233        | 110         |
+---------------------------------------+
| 128        | 25         | 412         |
+---------------------------------------+
|  284       | 284        | 130         |
+---------------------------------------+

All seams alright, however if we compare this with the array $data

$data = array(
    array('thin' => 156, 'heavy' => 128, 'total' => 284),
    array('thin' => 233, 'heavy' =>  25, 'total' => 284),
    array('thin' => 110, 'heavy' => 412, 'total' => 130),
);

you will see that in the first row it shows only thin

| 156 | 233 | 110 |

In the second row only shows heavy

| 128 | 25 | 412 |

and in the third row only shows total

| 284 | 284 | 130 |

When in reality should show something like this:

+ --------------------------------------+
|    Thin    |    Heavy   |    Total    |
+---------------------------------------+
|    156     |    128     |    284      |
+---------------------------------------+
|    233     |    25      |    284      |
+---------------------------------------+
|    110     |    412     |    130      |
+---------------------------------------+

then i realized, that maybe adding them below each other was the problem. so instead of using this on the odt

+-- ----------------------------------------------+
|           Thin           |  Heavy   |  Total    |
+-------------------------------------------------+
| [b.thin;                 |          |           |
| block=tbs:cell;          |          |           |
| parallel=tbs:table]      |          |           |
+-------------------------------------------------+
| [b.heavy]                |          |           |
+-------------------------------------------------+
|  [b.total]               |          |           |
+-------------------------------------------------+

I'am using this

+-- -------------------------------------------+
|    Thin             |    Heavy   |  Total    |
+----------------------------------------------+
|  [b.thin;           |  [b.heavy] | [b.total] |
|  block=tbs:cell;    |            |           |
|  parallel=tbs:table]|            |           |
|                     |            |           |
+----------------------------------------------+

Output:

+----------------------------------------------+
|          |         |         |       |       |
+----------------------------------------------+
|   128    |   25    |   412   |  522  |       |
+----------------------------------------------+

As you can see, it doesn't iterate over the array well, and generates blank columns, furthermore the data displayed is random

So if anyone knows what is wrong with this, please let me know

Thanks!

UPDATE

I realized that in [r.thin;block=tbs:cell;parallel=tbs:table] I use cell instead of row

So I tried to change it -> [r.thin;tbs:row;parallel=tbs:table],

It didn't work, However the first iteration is correct:

+ --------------------------------------+
|    Thin    |    Heavy   |   Total     |
+---------------------------------------+
| 156        | 128        | 284         |
+---------------------------------------+
haruk1515
  • 358
  • 2
  • 10

1 Answers1

1

The result you have is correct regarding to the parallel feature. The parallel feature performs a kind of merge by columns instead of merge by rows.

For a merge by rows, your template could be like this :

+---------------------------------------------------------------+
| Row number | Thin                     |  Heavy    |  Total    |
+---------------------------------------------------------------+
| [b.#]      | [b.thin;block=tbs:row]   | [b.heavy] | [b.total] |
+---------------------------------------------------------------+

For a merge by columns, your template could be like this :

+-- -----------------------+
|  Row number [b.#]        |
+--------------------------+
| [b.thin;                 |
| block=tbs:cell;          |
| parallel=tbs:table]      |
+--------------------------+
| [b.heavy]                |
+--------------------------+
|  [b.total]               |
+--------------------------+
Skrol29
  • 5,402
  • 1
  • 20
  • 25
  • It worked!!, now i can iterate over the array and generate the rows. Thanks a lot!! – haruk1515 Oct 28 '22 at 14:11
  • I have one more question if you don't mind, I decided to remove the columns name (thin,heavy,total) and make them dynamic too, so I was thinking to use block=tbs:cell, because I wanted to generate the names just one time, but it didn't work – haruk1515 Oct 28 '22 at 14:15
  • do you know some way to do it? – haruk1515 Oct 28 '22 at 14:16
  • You can delete some columns using parameter `ope=delcol`. See https://www.tinybutstrong.com/opentbs.php?doc#col It is better to delete columns before to merge the rows. – Skrol29 Oct 29 '22 at 20:58
  • No, I don't want to delete columns I want to make the headers of the columns dynamic(I found a solution ^^), the solution is using two arrays( one for the values, other for the name of the columns), for the values of the columns I used your solution, and for the names of the columns I used [a.headername;block=tbs:cell;] "headername" is the key of the array if someone is wondering -> $TBS->MergeBlock('a', $col_name); – haruk1515 Nov 08 '22 at 08:42