Report Parameters And Inputs Package
November 10, 2017KoolReport is designed to build dynamic reports which are able to receive parameters and display contents accordingly.
Input parameters to KoolReport
$report = new SaleReport(array(
"year"=>2016
));
$report->run()->render();
Let look at the bootstrap of above SaleReport
, the report will receive parameter which is the year 2016 and generate sale report for 2016. If we change "year"=>2017
, the report will create sale report for 2017.
Receive parameters inside KoolReport
The input parameters can be accessed in $this->params
variables in setup()
function.
class SaleReport extends \koolreport\KoolReport
{
...
function setup()
{
$this->src("mydata")
->query("select * from orders where year=:year")
->params(array(
":year"=>$this->params["year"]
))
->pipe($this->dataStore("orders_in_year"))
}
}
In above code, we see that the SQL query will change year according to the year parameters.
Now understanding the basic of KoolReport's parameters, you can create dynamic reports which are able to receive customized settings then generate report accordingly.
Inputs package
Inputs
package is an extended package of KoolReport to facilitate inputung parameters to your reports. Inputs package has built-in data binding mechanism and various input controls to build dynamic reports.
Example
<?php
//SaleReport.php
require "../koolreport/core/autoload.php";
class SaleReport extends \koolreport\KoolReport
{
use \koolreport\inputs\Bindable;
use \koolreport\inputs\POSTBinding;
protected function defaultParamValues()
{
return array(
"year"=>date("Y"),
);
}
protected function bindParamsToInputs()
{
return array(
"year"=>"yearInput",
);
}
function settings()
{
return array(
"dataSources"=>array(
"mydata"=>array(
"connectionString"=>"",
"username"=>"root",
"password"=>""
)
)
)
}
function setup()
{
$this->src("mydata")
->query("select * from orders where year=:year")
->params(array(
":year"=>$this->params["year"]
))
->pipe($this->dataStore("orders_in_year"))
}
}
<?php
//SaleReport.view.php
use \koolreport\widgets\koolphp\Table;
use \koolreport\inputs\TextBox;
?>
<h1ml>
<head>
<title>SaleReport</title>
</head>
<body>
<form method="post">
<label>Year</label>
<?php TextBox::create(array(
"name"=>"yearInput",
));?>
<button>Submit</button>
</form>
<?php
Table::create(array(
"dataStore"=>$this->dataStore("orders_in_year")
));
?>
</body>
</html>
<?php
//index.php
$report = new SaleReport;
$report->run()->render();
In above example, we demonstrate a sale report which receives year parameter from the input box and return list of orders for that particular year. The example use Bindable
and POSTBinding
to indicate that the year value will be sent to report by post
action from the form. The default year (is used in the first report load) is the current year, which is defined in the defaultParamValues()
function. The year
parameter will be binded to the yearInput
controls in the report's view with definition in bindParamsToInputs()
.
Summary
In this tutorial, you have learn how to insert parameters into KoolReport and how the Inputs package can help you to create dynamic reports faster.
If you have any question, you may reply to us of this email.
See you in the next tutorial.
Resources
<3 koolreport team