Categories > TinyButStrong general >

Sum value em dinamic col

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Valterci
Date: 2006-10-25
Time: 20:28

Sum value em dinamic col

Hi Skrol,

I need to show a data set in table with I number dinamico of columns. It has a field dates and when to move prints the addition of the field money of the current register for another line. It repeats until finishing the registers, always adding the conteudo it field money of each line and after zeroing to print.

Help me, please
By: Valterci
Date: 2006-10-25
Time: 21:28

Re: Sum value em dinamic col

Is this my function:

function f_total($Name,&$CurrRec,$RecNum) {
  global $save_data, $tot_1;
  if (!isset($save_data)) $save_data = '';
  if ($save_data!=$CurrRec['data']) {
    $tot_1 = 0;
    $save_data= $CurrRec['data'];
  }
  // Calculate totals
  $tot_1 += $CurrRec['valor'];

  // Save into the current record
  $CurrRec['tot_1'] = $tot_1;
}


is this my htm:



<table border='1' align="center" width="778px" class="bordasimples">
  <tr bgcolor="#FFFFFF">
    <td colspan="10"><div align="center"><b>C A I X A</b></div></td>
  </tr>
  <tr bgcolor="#CACACA">
    <td colspan="10"><div align="center"> ENTRADAS DE CAIXA CONSOLIDADAS DO PERÍODO DE [var.per_ini] ATÉ [var.per_fim]</div></td>
<tr><td>[blk.data;block=tr;headergrp=data;ondata=f_total]</td></tr>
<tr bgcolor="#F0F0F0">
    <td width="100"><div align="center"><strong>[blk_1.moeda;block=td]</strong><br>
        <span class="text-mini">[blk_1.valor;frm='000.000,00';block=td]</span><br>
      </div></td>
    <td  width="100"><div align="center"><strong>[blk_2.moeda;block=td]</strong><br>
        <span class="text-mini">[blk_2.valor;frm='000.000,00';block=td]</span><br>
      </div></td>
    <td  width="100"><div align="center"><strong>[blk_3.moeda;block=td]</strong><br>
        <span class="text-mini">[blk_3.valor;frm='000.000,00';block=td]</span><br>
      </div></td>
    <td  width="100"><div align="center"><strong>[blk_4.moeda;block=td]</strong><br>
        <span class="text-mini">[blk_4.valor;frm='000.000,00';block=td]</span><br>
      </div></td>
    <td  width="100"><div align="center"><strong>[blk_5.moeda;block=td]</strong><br>
        <span class="text-mini">[blk_5.valor;frm='000.000,00';block=td]</span><br>
      </div></td>
    <td  width="100"><div align="center"><strong>[blk_6.moeda;block=td]</strong><br>
        <span class="text-mini">[blk_6.valor;frm='000.000,00';block=td]</span><br>
      </div></td>
    <td  width="100"><div align="center"><strong>[blk_7.moeda;block=td]</strong><br>
        <span class="text-mini">[blk_7.valor;frm='000.000,00';block=td]</span><br>
      </div></td>
    <td  width="100"><div align="center"><strong>TOTAL</strong><br>
        <span class="text-mini">[var.tot_1;frm='000.000,00';block=td]</span><br>
      [blk;block=tr;serial]</div></td>

  </tr>

  <tr bgcolor="#F0F0F0">
    <td valign="middle">&nbsp;</td>
    <td  valign="middle" class="text-mini"><font color="#666666">This line is optional</font><font color="#666666">, it enables you to<br>
      define the empty cell.</font></td>
    <td valign="middle" bgcolor="#E4F1E4"><div align="center">
        <p><span class="text-mini">[blk_0;block=td]</span> <br>
          [blk;block=tr;serial]</p>
      </div></td>
  </tr>
</table>
By: Valterci
Date: 2006-10-25
Time: 21:31

Re: Sum value em dinamic col

The problem. The addition of variavel tot_1 tras so the addition of finishes line...
By: Skrol29
Date: 2006-10-26
Time: 02:15

Re: Sum value em dinamic col

Hi Valterci,

First I want to say that you're problem made me discover a small bug when displaying data with both parameters "headergrp" and "serial". It will be fixed in TBS 3.2.0.
The bug is that when a group have exactly the correct number of cells for a serial row, then an extra empty is displayed just after.

I think I understand your problem. With your template, the column Total should always display the same value. This is because [var.fields] are not merged at the same time as the block. They are merged at the end, when you call $TBS->Show().

I can suggest a solution which should work but is not smart:
In function f_total(), save the different totals in a global array.
function f_total($Name,&$CurrRec,$RecNum) {
  global $save_data, $tot_1, $tot_all;
  if (!isset($save_data)) {
    $save_data = '';
    $tot_all = array();
  }
  if ($save_data!=$CurrRec['data']) {
    if (isset($tot_1)) $tot_all[] = $tot_1;
    $tot_1 = 0;
    $save_data= $CurrRec['data'];
  }
  // Calculate totals
  $tot_1 += $CurrRec['valor'];

  // Save into the current record
  $CurrRec['tot_1'] = $tot_1;
}

Then, instead of displaying [var.tot_1] in your template, you can display all items of $tot_all in sequence. This can be done only using another function I think:
[var.tot_all;onformat=f_seq;frm='000.000,00';block=td]
function f_seq($FieldName,&$CurrVal) {
  global $tot_id, $tot_all;
  if (!isset($tot_id)) $tot_id = 0;
  $CurrVal = $tot_all[$tot_id];
  $tot_id++;
}

This method should be arrange if it happens that you have several lines in a same group.
By: Valterci
Date: 2006-10-26
Time: 03:57

Re: Sum value em dinamic col

Oi Skrol,
Its solution is excellent. But, the last total does not show. Or either, example: if I have 5 valores(totais), shows 4 and 5º does not show.
It can help me?