Categories > OpenTBS with ODT >

Multidimensional array and conditional

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Nino
Date: 2012-12-18
Time: 17:08

Multidimensional array and conditional

Hi,

I'm trying to get a remind letter for each supporter of a charity with a table showing all payments of each one.
I put the data from a database in an array  like the following:

[0] => Array
    (
    [0] => Array
        (
        [surname] =>
        [name] => Monaci
        [sex] => X
        [address] => Blera
        )
        [1] => Array
        (
         [project] => Array
         (
          [name] => Lijimbe
          [dates] => Array
          (
           [0] => 14-12-2007
           [1] => 23-12-2008
           [2] => 03-01-2011
          )
         )
        )
       )

[1] => Array
    (
    [0] => Array
        (
        [surname] => Altieri
        [name] => Salvatore
        [sex] => M
        [address] => Portici
        )
    [1] => Array
        (
        [project] => Array
         (
          [name] => Kevin
          [dates] => Array
          (
           [0] => 26-03-2010
           [1] => 11-02-2011
           [2] => 02-03-2012
          )
         )
        )
    [2] => Array
        (
        [project] => Array
         (
         [name] => Chikweo
         [dates] => Array
          (
           [0] => 26-03-2010
           [1] => 11-02-2011
           [2] => 02-03-2012
          )
         )
        )
    )

...etc....


I would like to obtain letters like  the following:

To:
Monaci
Blera

Dears,

We would like to remind you that you can pay the next contributions for the projects: Lijimbe

Here are your previous contributions:
Lijimbe
           No. Date
           1: 14-12-2007
           2: 23-12-2008
           3: 03-01-2011

To:
Salvatore Altieri
Portici

Dears,

We would like to remind you that you can pay the next contributions for the projects: Lijimbe

Here are your previous contributions:
Kevin
           No. Date
           1: 26-03-2010
           2: 11-02-2011
           3: 02-03-2012

Chikweo
           No. Date
           1: 26-03-2010
           2: 11-02-2011
           3: 02-03-2012

This is my tentative template:


To:
[onshow.name] [onshow.surname]
[onshow.address]

[onshow.sex;if [val]='M';then 'Dear '[onshow.name];else 'Dears'],

We would like to remind you that you can pay the next contributions for the projects: [b0.project]

Here are your previous contributions:
[b0.project]
           No. Date
           1: [b0.dates]

and this my php code:

function busteA4tbs($array){
    $data=$array;
    include_once('tbs/tbs_class.php');
    include_once('tbs/plugins/tbs_plugin_opentbs.php');
    $TBS = new clsTinyButStrong;
    $TBS->Plugin(TBS_INSTALL, OPENTBS_PLUGIN);
   
    $template = 'odt/letters.odt';
    $TBS->LoadTemplate($template,OPENTBS_ALREADY_UTF8);
   
        $TBS->MergeBlock('s', $data);   
   
    // delete comments
    $TBS->PlugIn(OPENTBS_DELETE_COMMENTS);
   
    // Define the name of the output file
    $file_name = str_replace('.','_'.date('Y-m-d').'.',$template);
    $file_name = str_replace('.','_'.$suffix.'.',$file_name);
    $TBS->Show(OPENTBS_FILE, $file_name);
}

Unfortunately it doesn't work!
Only the first two lines worked as expected.

Can you help me, please?

Thank you very much,
Nino
By: Skrol29
Date: 2012-12-19
Time: 00:51

Re: Multidimensional array and conditional

Hi Nino,

Your PHP script is quite OK but the data are not simply structured, thus is it not convenient for the merging.
I suggest that you build a variable which is a of the data but with a different structure, such as this one:
Array(
    [0] => Array(
        [surname] =>
        [name] => Monaci
        [sex] => X
        [address] => Blera
        [projects] => Array(
            [0] => Array(
                [name] => Lijimbe
                [dates] => Array(
                   [0] => 14-12-2007
                   [1] => 23-12-2008
                   [2] => 03-01-2011
                )
            )
        )
    )
    [1] => Array(
        [surname] => Altieri
        [name] => Salvatore
        [sex] => M
        [address] => Portici
        [projects] => Array(
            [0] => Array(
                [name] => Kevin
                [dates] => Array(
                    [0] => 26-03-2010
                    [1] => 11-02-2011
                    [2] => 02-03-2012
                )
            )
            [1] => Array(
                [name] => Chikweo
                [dates] => Array(
                    [0] => 26-03-2010
                    [1] => 11-02-2011
                    [2] => 02-03-2012
                )
            )
        )
    )
)

In your template, several thinks are wrong.
You want to repeat each support on a new page, each Supporter having several Projects, and each project having several Contributions.
So, for Supporters
, you have to use a block instead of [onshow] tags.

For example:
[s.name;block=tbs:page] [s.surname]
[s.address]
The keywork "tbs:page" is available since OpenTBS 1.8.0 which is in beta version but quite stable. Can be downloaded here: http://www.tinybutstrong.com/news.php
See the ODT example for merging with pages.

The you have to merge the Projects names. This is done with sub-blocks.
With the new data structure, you can use automatic sub-blocks (see the TBS manual).
And you can also use automatic sub-blocks for Contributions. Is should be a sub block of Projects.




By: Nino
Date: 2012-12-19
Time: 20:11

Re: Multidimensional array and conditional

Hi Skrol29,

thank you very much for you kind and quick reply.

Unfortunately I didn't succeeded to use new keywork, but  restructing my data I did succeed to get half of my letter!

I've modified  my template:

[s;block=begin;comm=text:p]
To:
[s.name] [s.surname]
[s.address]

[s.sex;if [val]='M';then 'Dear '[s.name];else 'Dears'],

We would like to remind you that you can pay the next contributions for the projects: [b0.project.name]
[s.sex;if [val]='M';then 'Dear '[s.name];else 'Dears'], please contact us for more information on our new projects.

Here are your previous contributions:
[b0.project.name]
           No. Date
           1: [b0.project.date]
[s;block=end;comm=text:p]

I looked at the TBS manual, but I wasn't able to use blocks in order to get the list of the project inside the body of the letter and the contributions table.

Can you help me?

Greetings,
Nino

By: Skrol29
Date: 2012-12-20
Time: 01:20

Re: Multidimensional array and conditional

Hi Nino,

What is the structure of your data now?
By: Nino
Date: 2012-12-20
Time: 07:44

Re: Multidimensional array and conditional

Hi Skrol29,

it's exactly the one you suggested me.

I didn't change the PHP  code.

Nino
By: Skrol29
Date: 2012-12-21
Time: 00:03

Re: Multidimensional array and conditional

Hi,

Here is a plan that should work with the data structure I've submitted.
The block names 's' has an automatic sub-block named 's_sub1'.
Block  's_sub1' also has an automatic sub-block, it is named 's_sub1_sub1'.

The following plan assumes that "s_sub1" and "s_sub1_sub1" fields are placed inside a Word table.
[s;block=begin;comm=text:p;sub1=projects]
  To:
  [s.name] [s.surname]
  [s.address]

  [s.sex;if [val]='M';then 'Dear '[s.name];else 'Dears'],

  We would like to remind you that you can pay the next contributions for the projects: [b0.project.name]
  [s.sex;if [val]='M';then 'Dear '[s.name];else 'Dears'], please contact us for more information on our new projects.

  Here are your previous contributions:
  [s_sub1.project.name;block=w:tbl;sub1=dates]
           No. Date
           [s_sub1_sub1.#]: [s_sub1_sub1.val;block=w:tr]

[s;block=end;comm=text:p]
By: Nino
Date: 2012-12-21
Time: 17:43

Re: Multidimensional array and conditional

Hi Skol29,

I tryed to use your template, but I got the following message:
TinyButStrong Error in block's definition [s_sub1.name...]: at least one tag corresponding to w:tbl is not found.
Check opening tags, closing tags and embedding levels.

I didn't succeed to get information about "w:tbl" and "w:tr" in the manual, so I changed the template in:


[s_sub1.name;block=table:table;sub2=dates]
[s_sub1_sub2.#]: [s_sub1_sub2.val;block=table:table-row]


Now I get all the names of the projects, but not the dates:


Mwichumu
[s_sub1_sub2.#]: [s_sub1_sub2.val;block=table:table-row]

Kabeliti
[s_sub1_sub2.#]: [s_sub1_sub2.val;block=table:table-row]

Nyenje
[s_sub1_sub2.#]: [s_sub1_sub2.val;block=table:table-row]

... etc ...

Thank you for your kind help.
Nino
By: Skrol29
Date: 2012-12-22
Time: 10:20

Re: Multidimensional array and conditional

Hi,

"w:tbl" and "w:tr" were for Ms Office, sorry for that.
Your entities are ok.

For dates, replace "sub2=dates" with "sub1=dates" and replace "s_sub1_sub2" with "s_sub1_sub1".
Dates are not a second sub-block, they are a sub-sub-block.
By: Nino
Date: 2012-12-22
Time: 11:17

Re: Multidimensional array and conditional

Hi,

it works!

Thank you very much for your help and your very useful TBS.

Bye,
Nino