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

add row on runtime ods

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

By: Budi
Date: 2014-07-02
Time: 06:32

add row on runtime ods

is it possible to add row to ods file on the fly?
without using multidimensional array.

i have a really big dataset, that eats too much memory if placed in an array.

so my problem is like this

while($data)
{
//prepare my data into an array
$x[] = array('no' => $data->indeks, 'name' => $data->name);
}
$TBS->MergeBlock('dt', $x);
and my template gonna be like this
[dt.no;ope=tbs:num;block=tbs:row]    [dt.name]
problem with that is the array gets so big. is it possible to merge the row in while statement?
like this
while($data)
{
//prepare my data into an array
$x = array('no' => $data->indeks, 'name' => $data->name);
$TBS->MergeBlock('dt', $x);
}
By: Skrol29
Date: 2014-07-03
Time: 03:17

Re: add row on runtime ods

Hi Budi,

If your object $data do inherits from Iterator or ArrayObject, then it can be directly given to MergeBlock() since TBS version 3.5.0.
$TBS->MergeBlock('dt', $data);

Otherwise you can create a small database plug-in that will be a reader for your variable $data.
See http://www.tinybutstrong.com/manual.php#plugins_db

By: Budi
Date: 2014-07-03
Time: 06:56

Re: add row on runtime ods

well, actually my $data is a mysql PDO query and it needs to be process in some way, before i get the correct data for the report.
this is the actual code looks like
while ($row = $queryIb->fetch())
{
    if($item == 0)
    {
        $pPel->execute(array($row->kodePel));
        $temp = $pPel->fetch();
        if($temp)
            $extra = $temp;
        else
        {
            $extra->namaPel = $row->kodePel;
            $extra->alamat = $row->kodePel;
        }
       
        if($sales[$row->kodeSales])
            $extra->sales = $sales[$row->kodeSales];
        else
            $extra->sales = $row->kodeSales;
       
        if($divisi[$row->divisi])
            $extra->divisi = $divisi[$row->divisi];
        else
            $extra->divisi = $row->divisi;
       
        $psIb->execute(array('noSJ' => $row->noSJ));
        $item = $psIb->fetch()->ct;

        $totPot = $row->pot;
       
        $persenPot = $totPot / $row->total;
       
        if($row->inc == 0)
        {
            if($app['pelAktif'] == 0)
            {
                $totDPP = $row->total - $totPot;
                $totPPN = 0;
            }
            else
            {
                $totDPP = $row->total - $totPot;
                $totPPN = floor($totDPP * 0.1);
            }
        }
        else
        {
            if($app['pelAktif'] == 0)
            {
                $totDPP = $row->total - $totPot;
                $totPPN = 0;
            }
            else
            {
                $totDPP = round(($row->total - $totPot) / 1.1);
                $totPPN = $row->total - $totPot - $totDPP;
            }
        }
    }
   
    $jmlHarga = round($row->jml * $row->harga);
   
    if($item == 1)
    {
        $pot = $totPot;
        $dpp = $totDPP;
        $ppn = $totPPN;
    }
    else
    {
        $pot = round($jmlHarga * $persenPot);
        if($row->inc == 0)
        {
            if($app['pelAktif'] == 0)
            {
                $dpp = $jmlHarga - $pot;
                $ppn = 0;
            }
            else
            {
                $dpp = $jmlHarga - $pot;
                $ppn = floor($dpp * 0.1);
            }
        }
        else
        {
            if($app['pelAktif'] == 0)
            {
                $dpp = $jmlHarga - $pot;
                $ppn = 0;
            }
            else
            {
                $dpp = round(($jmlHarga - $pot) / 1.1);
                $ppn = $jmlHarga - $pot - $dpp;
            }
        }
    }
   
    $totPot -= $pot;
    $totDPP -= $dpp;
    $totPPN -= $ppn;
   
    $dt = array('no'        => ($i+1),
                'tglSJ'        => $row->tglSJ,
                'noFK'        => substr($row->noFK, 4),
                'noPjk'        => $row->nPjk,
                'noTbpb'    => $row->noSJ,
                'kdPel'        => $row->kodePel,
                'namaPel'    => $extra->namaPel,
                'almtPel'    => $extra->alamat,
                'nmBrg'        => $row->namaBrg,
                'merk'        => $row->merk,
                'tipe'        => $row->tipe,
                'uk'        => $row->ukuran,
                'jml'        => $row->jml,
                'sat'        => $row->satuan,
                'hs'        => $row->harga,
                'jh'        => $jmlHarga,
                'pot'        => -$pot,
                'th'        => ($jmlHarga - $pot),
                'dpp'        => $dpp,
                'ppn'        => $ppn,
                'ha'        => ($dpp + $ppn),
                'sal'        => $extra->sales,
                'div'        => $extra->divisi
            );
   
    if($_GET['tb'])
    {
        $dt[$i]['ref'] = $row->noRef;
        $dt[$i]['noSO'] = $row->noSO;
    }
       
   
    $item --;
    $i++;
}
$TBS->MergeBlock('dt', $dt);

that code works, but the array created is way to big.
By: Budi
Date: 2014-07-04
Time: 12:05

Re: add row on runtime ods

is it possible to make it function like this

search for the tag, insert a copy above tagged row, merge the data, when show is called all block tag is removed.

tried reading the code of tbs and open tbs, can't find a clue about how to implement it. haha
By: Skrol29
Date: 2014-07-04
Time: 12:55

Re: add row on runtime ods

Your snippet says "$dt = array(...)"
Is this correct or should it be "$dt[$i] = array(...)" ?

In the first case you cannot merge row per row because your recordset needs to be entirely read before to display the result.
In the second case, you can merge row per row.

> is it possible to make it function like this

Yes in the second case.
This is in the TBS level, not OpenTBS.
Use the technical described here: http://www.tinybutstrong.com/manual.php#plugins_db

For example:

$TBS->MergeBlock('dt', 'BUDY', $queryIb);

// -------------
// Custom data-plugin for keyword 'BUDI'
// -------------

function tbsdb_BUDI_open(&$Source, &$Query) {
   return $Query;
}

function tbsdb_BUDI_fetch(&$Rs)  {
   $row = $Rs->fetch();
   if (!$row) return false;
   // your code here
   ...
   $dt = array('no'     => ($i+1),
               'tglSJ' => $row->tglSJ,
               ...
   );
   ...
   return $dt;
}

function tbsdb_BUDI_close(&$Rs)  {
  // nothing to do
}

By: Budi
Date: 2014-07-04
Time: 16:55

Re: add row on runtime ods

ah, yes it's indeed
$dt[$i] = array(...);

i see, so i can make the calculation in the db fetch
thanks for the help Skrol29, will look into it :D
By: Budi
Date: 2014-07-07
Time: 13:15

Re: add row on runtime ods

hmm, tried using plugins method but still running into memory limit problem.

is it still using array before merging with the xml file?
is there anyway to overcome this?


before this i was using a simple xls where the output is just echoed with specific header and never run to a memory limit problem.
http://www.appservnetwork.com/modules.php?name=News&file=article&sid=8