KoolReport's Forum

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

How to use ArrayDataSource? #10

Open KoolReport opened this topic on on May 2, 2017 - 10 comments

KoolReport commented on May 2, 2017

In many cases, you have your own data stored in PHP array. The data could be result of your previous processes and we would like to input those data to report:

<?php
//Our sample data in array
$myData  = array(
    array("name"=>"Peter","age"=>30),
    array("name"=>"Mary","age"=>25),
);
$report = new MyReport(array(
    "data"=>$myData    //Insert data into MyReport as parameters.
));
$report->run()->render();

In the settings() function of MyReport, you can set the data source like this

public function settings()
{
    return array(
        "dataSources"=>array(
            "mydata"=>array(
                "class"=>"\koolreport\datasources\ArrayDataSource",
                "data"=>$this->params["data"],
                "dataFormat"=>"associate"
            )
        )
    );
}

And in the setup() function, you can do:

protected function setup()
{
    $this->src('mydata')
   ...
    ->pipe($this->dataStore("test"))
}

Please leave your comment.

Marty commented on May 9, 2017

Thank you!

Marty commented on May 9, 2017

Can it use with array format like this:

array(
    array("name","age"),
    array("Peter",30),
    array("Mary",25),
)

or I have to convert it?

Peter Williams commented on May 10, 2017

Hi Marty,

You set the "dataFormat"=>"table"

<?php
class MyReport extends \koolreport\KoolReport
{
    public function settings()
    {
        return array(
            "dataSources"=>array(
                "array_example_datasource"=>array(
                    "class"=>'\koolreport\datasources\ArrayDataSource',
                    "dataFormat"=>"table", //Table data format
                    "data"=>array(
                        array("customerName","dollar_sales"),
                        array("Johny Deep",100),
                        array("Angelina Jolie",200),
                        array("Brad Pitt",200),
                        array("Nocole Kidman",100),
                    )
                ),
            )
        );
    }
}

Hope that helps. If you have any problem please let us know.

Regards,

Peter

KoolReport commented on Sep 19, 2017

With the latest version of KoolReport, you can use the load() to load array in the setup:

$this->src('array_example_datasource')->load(array(
    array("name"=>"Peter","age"=>30),
    array("name"=>"Karl","age"=>25),
))
->pipe(...)
...
mike commented on Aug 7, 2018

hi, Can I send data using array for date selection ...

$myData  = array(
    array("created_at"=>"2018-07-17","end_date_time"=>'2018-12-31'),
);
$report = new MyReport(array(
    "data"=>$myData    //Insert data into MyReport as parameters.
));
$report->run()->render();
 protected function defaultParamValues()
    {   
        return array(
		"dateRange"=>array(
			  date("Y-m-d",strtotime($this->params['mydata']['created_at'])),
				date("Y-m-d",strtotime( $this->params['mydata']['end_date_time']))
               ),
            );
    }

I throws an error .......

KoolReport commented on Aug 7, 2018

You sent the report params "data" but use "mydata" in the report, so here is the solution

$report = new MyReport(array(
    "mydata"=>$myData    //Insert data into MyReport as parameters.
));
mike commented on Aug 7, 2018

protected function defaultParamValues()

{   
    return array(
	"dateRange"=>array(
		  date("Y-m-d",strtotime($this->params['data']['created_at'])),
			date("Y-m-d",strtotime( $this->params['data']['end_date_time']))
           ),
        );
}

dateRange is not taking value....

mike commented on Aug 8, 2018

is there other setting for pdf download ......with arraydatasource...

<?php

$myData  = array(
    array("name"=>"Peter","age"=>30),
    array("name"=>"Mary","age"=>25),
);
$report = new MyReport(array(
    "data"=>$myData    //Insert data into MyReport as parameters.
));
$report->run()
->export('MyReport_pdf')
	->pdf(array(
				"format"=>"A4",
				"orientation"=>"portrait"
			))
			->toBrowser("Report.pdf");

its giving me error ----no data available...

please help with this .....its very urgent

KoolReport commented on Aug 9, 2018

Could you please your all of your code

mike commented on Aug 10, 2018

Thanks so much for your source ..

I have solved the problem ...

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