KoolReport's Forum

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

How to insert parameters to KoolReport? #1

Open KoolReport opened this topic on on Apr 25, 2017 - 10 comments

KoolReport commented on Apr 25, 2017

KoolReport is able to create dynamic report which received parameters from outside. Let say we want to create a SaleReport which summarize sales for each months of a given year.

We can do this:

<?php

$saleReport = new SaleReport(array(
    "year"=>2005
));
$saleReport->run()->render();

Here is our SaleReport.php setup:

<?php
class SaleReport extends from \koolreport\KoolReport
{
    ...
    function setup()
    {
        //In here you can get the year from $this->params["year']

        $this->src("sales")->query("
            SELECT month,sum(sale)
            FROM tbl_sales
            WHERE year=:year
            GROUP BY month
        ")
        ->params(array(
            ":year"=>$this->params["year"]
        ))
        ->pipe($this->dataStore('month-of-year'));
    }

}

The example show how KoolReport can get parameters and access it in setup() or setting() function.

JTBB commented on Mar 3, 2019

I am a newbie ... I find it hard to get this exact example working ... only errors when I copy and paste ... please put full working example ..I bought the input package hoping to get better info then on the website but its exactly the same ... please put simple and complete working examples ... I only want to pass the parameter from code ...

With this I assume you are able to pass a variable:

$saleReport = new SaleReport(array( "year"=>2005));
$saleReport->run()->render();

So I am trying everything I know. Its obvious I have not enough knowledge. I can't get this to work:

$report = new MyReport(array("RelationOrderID"=>"xyz"));
$report->run()->render();

thank you!

KoolReport commented on Mar 4, 2019

If you do:

$report = new MyReport(array("RelationOrderID"=>"xyz")); 
$report->run()->render();

Then you can get the parameter inside the setup() function of MyReport like this:

class MyReport extends \koolreport\KoolReport
{
    function setup()
    {
        $this->src("mysource")->query("
            SELECT * FROM mytable
            WHERE
            RelationOrderID = :RelationOrderID
        ")
        ->params(array(
            ":RelationOrderID"=>$this->params["RelationOrderID"]
        ))
        ->pipe($this->dataStore("data"));
    }
}

In the setup function, as you can see above, we perform SQL statement with parameter :RelationOrderID and bind it with the parameter that you enter, in this case will be "xyz".

KoolReport commented on Mar 4, 2019

Is it possible that you create a separate topic, I will answer you on the new topic.

Andrew Borell commented on Mar 5, 2019

Adding an advanced dynamic approach in the event you may not know which parameters the user will submit because I use this logic quite a bit with koolreport.

class MyReport extends \koolreport\KoolReport
{
    function setup()
    {
	// initialize variable and array  
	$relationOrderId	= '';
	$params 		= array();

	if(array_key_exists('RelationOrderId',$this->params)){
		$relationOrderId .= 'and relationOrderId = :relationOrderId';
		$params[':relationOrderId'] = $this->params["RelationOrderId"];
		}
	$this->src("mysource")->query("
		SELECT * FROM mytable
		WHERE 	( 
			RelationOrderID = '-1' 
			{$relationOrderID}
			)	
		")
	->params($params)
	->pipe($this->dataStore("data"));
    }
}

I use -1 in the condition because auto-increment would start at 0 and intend to return no results unless it matches a relationOrderId. In some cases using null is a better choice.

Add as many variables and group as you see fit in the conditions or a join as needed. The condition and parameter will only be added if it exists when creating the instance of the object. e.g.)

$report = new MyReport(array("RelationOrderID"=>"xyz")); 
$report->run()->render();

This is much more useful in a situation where you have multiple parameters coming from a form of course. e.g.)

$p = array();
if(isset($_POST['RelationOrderId'])
{
	$p['RelationOrderId'] = $_POST['RelationOrderId'];
}
if(isset($_POST['RelationOrderStatus'])
{
	$p['RelationOrderStatus'] = $_POST['RelationOrderStatus'];
}

$report = new MyReport($p); 
$report->run()->render();
JTBB commented on Mar 5, 2019

@ Andrew ... this is very useful and thank you for adding this.

piyush chandak commented on Jan 14, 2020

can we send more than one array when we create object

KoolReport commented on Jan 14, 2020

The parameter you send to report is array, and the array can have as many element inside as you want.

piyush chandak commented on Jan 14, 2020

on creating our own function other than setting and setup then its giving error ?? is it possible ??

KoolReport commented on Jan 14, 2020

Could you please create a new topic and give us details what you need?

Andrew Borell commented on Jan 14, 2020

If you must pass two arrays then why not combine them into a single multidimensional array and sort it out in the view? Also, start a new topic to discuss.

thx -d

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
wiki

None