KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.
Forum's Guidelines

Dynamic Report with Outside Parameters with CSV datasource #1298

Closed hsw opened this topic on on Feb 13, 2020 - 7 comments

hsw commented on Feb 13, 2020

Hi KoolReport,

I need to use outside parameters to build dynamic reports. I found a useful example in forum topic: https://www.koolreport.com/forum/topics/1 This example is using MySQL as datasource.

Can I have an example that is using CSV as datasource, and Filter process to reference the parameter as criteria in the setting() function.

Thank you

KoolReport commented on Feb 13, 2020

It is very easy since all parameters you set to report will be available in settings() and setup(). For example, you build a DynamicReport which will receive 2 parameters, first is the csvFile and second is the year for Filter. You will initiate report like this:

$report = new DynamicReport([
    "csvFile"=>"../data/sale.csv",
    "year"=>2018
]);
$report->run()->render();

Your DynamicReport.php will be something like this:

class DynamicReport extends \koolreport\KoolReport
{
    function settings()
    {
        return [
            "dataSources"=>[
                "csvData"=>[
                    "class"=>'\koolreport\datasources\CSVDataSource',
                    'filePath'=>$this->params["csvFile"]
                ]
            ]
        ];
    }

    function setup()
    {
        $this->src("csvData")->pipe(new \koolreport\processes\Filter([
            "year"=>$this->params["year"]
        ]))
        ->pipe($this->dataStore("result"));
    }
}

Hope that helps.

hsw commented on Feb 14, 2020

Hi KoolReport,

Thanks for your prompt reply and the example given. It helps a lot.

Next I need to do use the parameter value for calculation, using CalculatedColumn. Example, duration = 2020 - year parameter. Can you also show an example.

Thank you

KoolReport commented on Feb 14, 2020

You do:

//Save the parameter year
$year = $this->params["year"];

$this->src("source")->query("....")
->pipe(new CalculatedColumn([
    "duration"=>function($row) use($year) {
        return 2020 - $year;
    }
]))

you will have new column name "duration" with value = 2020 - year parameter

hsw commented on Feb 17, 2020

Thank you for the examples.

I want to go further to use the parameter at the xxx.view.php file, to output dynamic Report Title. Can this be done?

Thank in advance.

KoolReport commented on Feb 17, 2020

Yes, at the view of report, you still can use $this->params["year"]

hsw commented on Feb 18, 2020

Got it. Thanks very much.

KoolReport commented on Feb 18, 2020

Great

Build Your Excellent Data Report

Let KoolReport help you to make great reports. It's free & open-source released under MIT license.

Download KoolReport View demo
help needed
solved

None