Skip to content

Do something beyond standard data source

Ks Tan edited this page Sep 6, 2019 · 17 revisions

1. Using dynamic sql, which is not exists in jrxml file:

$sql="SELECT * FROM table1";
$PHPJasperXML->load_xml_file('file1.jrxml'); 
$PHPJasperXML->sql = $sql;  

2. Using flexible data using array:

$data=[
    ["id"=>1,"name"=>"John","age"=>30],
    ["id"=>2,"name"=>"Mr Lee","age"=>23],
];
$PHPJasperXML->load_xml_file('file1.jrxml'); 
$PHPJasperXML->setData($data);

If you wish to use xml, or json, then just convert it as array pattern like $data = json_decode($jsondata,true);. Then can use same method. If your json pattern having complex hierarchy, can refer (4).

3. Use php variable/function instead of jasper report rules

In text field, usually you define something as $F{column1}, that is follow Jaspersoft standard. In fact, it is very restricted and lot of time insufficient for complex case. We can do something as when necessary:

$mycompanyname="Sim It Sdn Bhd";
function mytime(){
 return date('Y-m-d');
}
...load phpjasperxml...

then, in jrxml's text field. you can simply pass php function or variable such as $mycompanyname or mytime(), str_replace('Sdn Bhd','S/B',$mycompanyname). It is very flexible but use this in your own risk,

4. Access data all data easily, across all rows.

Sometimes, we may have requirement to access next row record or previous row record during we define "Print When Expression". No matter we wish to show the value in text field, or use in "Print When Expression". We can by pass Jasper Report rules via direct access into php variable in our object.
Example,
a. Wish to show first row, column1's value into text field, then we define this $this->arraysqltable[0]["column1"]
b. Wish to get previous row column1's value: $this->arraysqltable[($this->global_pointer-1)]["column1"]
c. We can use php variable in print when expression too, like hide same data if column value diff with previous row:
$this->arraysqltable[($this->global_pointer-1)]["column1"] != $this->arraysqltable[($this->global_pointer)]["column1"]

Clone this wiki locally