Categories > TinyButStrong general >

Dynamic table (using as a GRID) - Very important for me!

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: flaviosteffens
Date: 2006-11-10
Time: 19:49

Dynamic table (using as a GRID) - Very important for me!

Hi all,

Im trying to do this kind of grid:


Company/Resource      Month 1      Month 2      Month n      Totals
---------------------------------------------------------------------------
Company ABC
Computers                  US$104       US$406      ...              US$508
People                        US$50         US$70       ...              US$120
TOTALS                       US$154       US$476     ...              US$628
Company XYZ
Papers                        US$10         US$15       ...              US$25
People                        US$100       US$50       ...              US$150
TOTALS                       US$110       US$65       ...              US$175
---------------------------------------------------------------------------
TOTALS                       US$ ...         US$ ...      ...              US$....


(Hope this show well in the text).

I could make the company ABC appear with the rows/columns - using the example of dynamic table. But I could not make the other company appear in the "grid".

How can I make this kind of grid? Is it possible?
By: flaviosteffens
Date: 2006-11-10
Time: 21:13

Re: Dynamic table (using as a GRID) - Very important for me!

Oh, Im trying to use something like this:

An array to the name of COMPANY (1, 2, 3, ...)

An array to the RESOURCES (MONTH and VALUE)

An array to the TOTALS.

So, in the example:


$company1 = array ("name" => "Company ABC");
$resources1 = array ("Computers" => array ("Month1"=> 104, "Month2"=>406"), "People"=> array("Month1" => 50, "Month2"=>70));
$totals1 = array ("Month1" => 154, "Month2" => 476);

and so on...

By: Skrol29
Date: 2006-11-11
Time: 05:01

Re: Dynamic table (using as a GRID) - Very important for me!

Hi,

I think I would choose to do the totals with the Aggregate plug-in. But it doesn't work with rupture (headergrp or footergrp). So I would use subblocks instead of headergrp / footergrp.

Let put the problem of columns and global total in parentheses for a moment.
The template could be something like this:
Company/Resource
<tr> -------------------------------------------
<tr> Company [c.comp_name;block=tr+tr+tr]
<tr> [r.ress;block=tr;aggregate=amount:sum;p1=[c.comp_id]]
<tr> TOTALS
<tr> -------------------------------------------

Now for dynamic columns :
You have to rebuild a new data array to feet with the TBS template with the same philosophy of the Example about Dynamic columns.

Here is what it can be (please only take the idea, the code is not tested)
$qry = mysql_query('SELECT MONTH(date) AS month , SUM(price) AS amount , comp_id, comp_name , ress FROM table1 GROUP BY comp_id, comp_name , ress_name ');
$month_lst = array();
$comp_lst = array();
$big_total = array();

while ($rec = mysql_fetch_array($qry)) {
    $m = $rec['month'];
    $c = $rec['comp_id'];
    // update list of month
    if (!isset($month_lst[$m])) $month_lst[$m] = $m;
    // update list of company
    if (!isset($comp_lst[$c])) $month_lst[$c] = array('comp_id'=>$c , 'comp_name'=> $rec['comp_name']; , 'row'=>array() );
    // save the cell information
    $row =& $comp_lst[$c]['row'] // take car of the assignation by reference with &
    $row['a_'.$m] = $rec['amount'];
    $row['ress'] = $rec['ress'];
    // big total
    if (!isset($big_total[$m)) $big_total[$m = 0.0;
    $big_total[$m] = $big_total[$m] + $rec['amount'];
}

$TBS->MergeBlock('m1,m2,m3,m4',$month_lst); // Dynamic columns, us as many m1,m2... as they are rows in the table before the merge
$TBS->MergeBlock('c',$comp_lst); // Merge company as main block
$TBS->MergeBlock('r','array','comp_lst[%p1%][row]'); // Merge cells as subblock

// Merge total
...

In this case, some of your row may have not any amount for each month. So the columns may be missing for each celle. Use parameter "noerr" to avoid this problem.

I suggest that you first test this solution without activating the subtotals and the Aggregate plug-in. It may not works with missing cells, but you can arrange that in the plug-in source code.