KoolReport's Forum

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

Using an array as a datsource #1520

Open Richb201 opened this topic on on Jul 9, 2020 - 2 comments

Richb201 commented on Jul 9, 2020

I am trying to use an array as a datasource. The array is located at $_SESSION[result]. Here is part of the settings in MyReport.php

       "dataSources"=>array(                                <<<this is line 72
        "array_$result_datasource"=>array(
            "class"=>'\koolreport\datasources\ArrayDataSource',
            "dataFormat"=>"associate",
            "data"=>array("$_SESSION['result']))")
            ->pipe($this->dataStore("results"))
            ->saveTo($AR);

I used the example and then just modified the array to be mine. But when I try to run this I get:

An uncaught Exception was encountered Type: ParseError

Message: syntax error, unexpected '=>' (T_DOUBLE_ARROW)

Filename: /app/assets/MyReport.php

Line Number: 72

KoolReport commented on Jul 9, 2020

You can do something like this:

    public function settings()
    {
        return array(
            "dataSources"=>array(
                "data"=>array(
                    "class"=>'\koolreport\datasources\ArrayDataSource',
                    "data"=>$_SESSION['result'],
                    "dataFormat"=>"associate",
                )
            )
        );
    }  

    protected function setup()
    {
        $this->src('data')
        ->pipe($this->dataStore('result'));
    } 

Richb201 commented on Jul 9, 2020

Thank you. I currently have the following in my settings and setup. Notice that I am using mySQL for much of the data in the report. I use the same datasource over and over again, and then saveTo() different variables. Honestly, I have no idea if this is valid since I am "flying blind". Can I have more than one dataSources array in settings (one for mySQL, and one for my $_SESSION['result'])? And how about setup? Can I just paste your code above below my existing setup?

 public function settings()
  {

        return array(

            "assets" => array(
                "path" => "/app/assets/koolreport_assets",
                "url" => "assets",
            ),

            "dataSources" => array(
                "substantiator" => array(
                    "connectionString" => "mysql:host=mysql;dbname=mysql_databases",
                    'host' => 'mysql',
                    'username' => 'admin',
                    'password' => 'xxx',
                    'dbname' => 'substantiator',
                    'charset' => 'utf8',  
                    'class' => "\koolreport\datasources\MySQLDataSource"  
                ),
            )
        );
    }

    function setup()
    {
        $campaign=$_SESSION['campaign'];
        $email=$_SESSION['userid'];
        $this->src('substantiator')
            ->query("SELECT bus_comp FROM business_components WHERE campaign='$campaign' AND email='$email'")
            ->pipe(new ColumnRename(array(
            "bus_comp"=>"Business Component")))
            ->pipe($this->dataStore("business_components"))
        ->saveTo($BC);

        $this->src('substantiator')
            ->query("SELECT project FROM project WHERE campaign='$campaign' AND email='$email'")
            ->pipe($this->dataStore("project"))
            ->saveTo($PR);

        $this->src('substantiator')
            ->query("SELECT cost_center_number, cost_center_name FROM cost_center WHERE campaign='$campaign' AND email='$email'")
            ->pipe(new ColumnRename(array(
                "cost_center_number"=>"number","cost_center_name"=>"name")))
            ->pipe($this->dataStore("cost_center"))
            ->saveTo($CC);

        $this->src('substantiator')
            ->query("SELECT description FROM campaigns2 WHERE company_division='$campaign' AND email='$email'")
            ->pipe(new ColumnRename(array(
                "description"=>"company")))
            ->pipe($this->dataStore("campaigns2"))
            ->saveTo($CA);

        $this->src('substantiator')
            ->query("SELECT employee,employee_title, w2_wages FROM employees WHERE campaign='$campaign'")
           // ->pipe(new ColumnRename(array(
           //     "description"=>"company")))
            ->pipe($this->dataStore("employees"))
            ->saveTo($EM);

        $this->src('substantiator')
            ->query("SELECT URL FROM images WHERE campaign='$campaign' AND email='$email'")
            // ->pipe(new ColumnRename(array(
            //     "description"=>"company")))
            ->pipe($this->dataStore("images"))
            ->saveTo($IM);
    }

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
None yet

None