TinyButStrong - the PHP Template Engine
Categories > OpenTBS with ODT >

Grouping multidimensional datas with tables

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs

By: Martin
Date: 2015-12-16
Time: 16:34

Grouping multidimensional datas with tables

Hi,

I'm struggling with my phone list. Actually I want to create a phone list from grouped by branches. So I have two arrays:

$arrBranches = array(
   array('BRANCH_ID' => 3, 'BRANCH_NAME' => 'Berlin'),
   array('BRANCH_ID' => 4, 'BRANCH_NAME' => 'Paris'),
   array('BRANCH_ID' => 5, 'BRANCH_NAME' => 'London')
);

$arrEmployees = array(
   array('BRANCH_ID' => 4, 'NAME' => 'Ford', 'FIRSTNAME' => 'Harrison', 'PHONE' => '12345'),
   array('BRANCH_ID' => 3, 'NAME' => 'Douglas', 'FIRSTNAME' => 'Michael', 'PHONE' => '34567'),
   array('BRANCH_ID' => 3, 'NAME' => 'Steward', 'FIRSTNAME' => 'Patrick', 'PHONE' => '89321')
);


Now I want to group the phone list by the branch name. The relations are done by the BRANCH_ID. For each branch there have to be an own table. My .odt looks like this:


[main;block=begin;sub]
Branch: [main.BRANCH_NAME]
---------------------------------------------------------------------------------------------------------
| NAME                                                           | PHONE                                |
---------------------------------------------------------------------------------------------------------
|[main_sub.FIRSTNAME;block=table:table-row;p1=[main.BRANCH_ID]]  | [main_sub.PHONE]                     |
---------------------------------------------------------------------------------------------------------
[main;block=end]


And here is the PHP part:
$TBS->MergeBlock('main', 'array', $arrBranches );// Main block
$TBS->MergeBlock('sub', 'array', $arrEmployees); // Sub block. $data must be a global variable to have this working

The output shows the branches above the table, but the table rows are not filled up with the employee datas.

Some help would be great.

Regards
Martin
By: Skrol29
Date: 2015-12-16
Time: 17:06

Re: Grouping multidimensional datas with tables

Hi Martin,

You cannot use sub-block with a sub-data structured as you have.
In sub-data, the parameter must be a key not a value.
Your snippet could work if you had:
$arrEmployees = array(
   4 => array(
      array('NAME' => 'Ford', 'FIRSTNAME' => 'Harrison', 'PHONE' => '12345'),
   ),
   3 => array(
      array('NAME' => 'Douglas', 'FIRSTNAME' => 'Michael', 'PHONE' => '34567'),
      array('NAME' => 'Steward', 'FIRSTNAME' => 'Patrick', 'PHONE' => '89321'),
   ),
);

$TBS->MergeBlock('main', 'array', $arrBranches );
global $arrEmployees;
$TBS->MergeBlock('sub', 'array', 'arrEmployees[%p1%]');

The best solution, I think, is to restructure your $arrBranches data and use automatic sub-block instead of sub-block with dynamic query.

That is :
$arrBranches = array(
   array('BRANCH_ID' => 3, 'BRANCH_NAME' => 'Berlin', 'employees' => array(
      array('BRANCH_ID' => 3, 'NAME' => 'Douglas', 'FIRSTNAME' => 'Michael', 'PHONE' => '34567'),
      array('BRANCH_ID' => 3, 'NAME' => 'Steward', 'FIRSTNAME' => 'Patrick', 'PHONE' => '89321')
   )),
   array('BRANCH_ID' => 4, 'BRANCH_NAME' => 'Paris', 'employees' => array(
      array('BRANCH_ID' => 4, 'NAME' => 'Ford', 'FIRSTNAME' => 'Harrison', 'PHONE' => '12345'),
   )),
   array('BRANCH_ID' => 5, 'BRANCH_NAME' => 'London', 'employees' => array()),

);

$TBS->MergeBlock('main', 'array', $arrBranches );

And at the template side:
Branch: [main.BRANCH_NAME;block=tbs:p+tbs:table;sub1=employees]
------------------------------------------------------------------------------
| NAME                                | PHONE                                |
------------------------------------------------------------------------------
|[main_sub1.FIRSTNAME;block=tbs:row]  | [main_sub.PHONE]                     |
------------------------------------------------------------------------------
By: Martin
Date: 2015-12-16
Time: 19:27

Re: Grouping multidimensional datas with tables

Cool! It's working now.

Thank you so much!
Martin