KoolReport's Forum

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

Passing a array into a datetime picker #1839

Closed Richb201 opened this topic on on Jan 11, 2021 - 30 comments

Richb201 commented on Jan 11, 2021

I have a report that has a datetime picker at the top. Here it is :

<form method="post">
    <?php
    DateTimePicker::create(array(
        "name" => "datePicker",
        "maxDate" => "@endDatePicker",
        "format" => "YYYYMM",
        "themeBase" => "bs4",
    ));
    ?>
    <button type="submit">Submit</button>
</form>

Instead of using a textbox to allow the user to enter a date (in the YYYYMM format) I'd like to use an array of allowed dates already in the proper format. I guess a pull down would work. I have a function called getTaxyear (in a model) that populates an array call $TY. How would suggest switching this over?

Richb201 commented on Jan 11, 2021

Using "trial and error" I added this:

BSelect::create(array(
'name'=>"my_select",
"placeholder"=>"Select Report",
"data"=>$this->MyModel->getTaxyear()
)
);

getTaxyear() returns an array of the possible taxyears. When I run this in my report I get this notice

Message: Undefined property: MyReport::$MyModel Filename: assets/MyReport.view.php Line Number: 72

and then this error: Message: Call to a member function getTaxyear() on null Filename: /app/assets/MyReport.view.php Line Number: 72

Can I call a function within a BSelect:: ??

Richb201 commented on Jan 11, 2021

Then I tried:

protected function defaultParamValues()
{

    return array(
        "datePicker" => $this->dataStore("campaigns23")->get(0,"taxyear"),
        "BSelect" => $this->MyModel->getTaxyear()
    );
}

But still no luck.

David Winterburn commented on Jan 13, 2021

Please print out the default value to make sure it's in correct format:

protected function defaultParamValues()
{
    var_dump($this->dataStore("campaigns23")->get(0,"taxyear"); echo '<br>';
    var_dump($this->MyModel->getTaxyear()); echo '<br>';
    return array(
        "datePicker" => $this->dataStore("campaigns23")->get(0,"taxyear"),
        "BSelect" => $this->MyModel->getTaxyear()
    );
}
Richb201 commented on Jan 13, 2021

I am not sure what you are getting at? Your code above is missing a ) on the first line. This is what I get: A PHP Error was encountered Severity: Notice

Message: Undefined property: MyReport::$MyModel

Filename: assets/MyReport.php

Line Number: 322

Backtrace:

File: /app/assets/MyReport.php Line: 322 Function: _error_handler

Richb201 commented on Jan 14, 2021

I created a session variable in my php controller called TY and in setup for the report I use

function settings()
{
    return array(
        "dataSources"=>array(
            "substantiator" => array(
                "connectionString" => "mysql:host=mysql;dbname=substantiator",
                "username" => "admin",
                "password" => "sadffg",
                "charset" => "utf8"
                                    ),
        ),
        "data"=>array(
            "class"=>'\koolreport\datasources\ArrayDataSource',
            "data"=>$_SESSION['TY'],
            "dataFormat"=>"table",
        )
    );
}

in the view I am trying to get a pulldown (this function is failing)

BSelect::create(array(
'name'=>"my_select",
"placeholder"=>"Select Report",
"data"=>"data"
)
Richb201 commented on Jan 14, 2021

This is getting silly. The only reason I paid for this package is that I thought that there was support. I guess not.

David Winterburn commented on Jan 15, 2021

I don't see you set the "dataSource" property for your BSelect input. How do you expect it to be populated? In case you use "data", its value should be an array of values instead of a string "data".

Richb201 commented on Jan 15, 2021

OK. Right now I have these two functions in the report

   protected function defaultParamValues()
    {
        $CI = & get_instance();
        $CI->load->model('MyModel')->library('session');

        return array(
            "datePicker" => $this->dataStore("campaigns23")->get(0,"taxyear"),
            "BSelect" => $_SESSION['TY']
        );
    }

    protected function bindParamsToInputs()
    {
        return array(
            "datePicker",
        );
    }

and then for the control itself I have the following in the view:
    BSelect::create(array(
    'name'=>"my_select",
    "placeholder"=>"Select Report Tax Year",
    "data"=>""
    )
    );

$_SESSION['TY'] is an array of strings. Where would i place $_SESSION['TY']?

David Winterburn commented on Jan 15, 2021

There are various problems with your code:

1 . The BSelect input's name is "my_select" but both your defaultParamValues() and bindParamsToInputs() don't have its name. One uses "BSelect" which is the type of the input, not its name, the other lacks "my_select" totally.

2 . When you get your BSelect name correct in the functions, decide whether it allows single or multiple selection:

https://www.koolreport.com/docs/inputs/bselect/#multiselect-in-bselect

If it's single choice, its default value in defaultParamValues() should be a string. If it's multiple choices, the default value should be an array of strings.

3 . After that, set the BSelect's "data" (with an array of strings) or "dataSource" (with a datastore)

As a good advice, I would suggest you copy and modify an input in our inputs example:

https://www.koolreport.com/examples/reports/inputs/intro/

and read our documentation carefully:

https://www.koolreport.com/docs/inputs/bselect/

instead of inventing your own code.

KoolReport commented on Jan 15, 2021

This part of your code has problem as well (you will never get data from src("data") )

function settings()
{
    return array(
        "dataSources"=>array(
            "substantiator" => array(
                "connectionString" => "mysql:host=mysql;dbname=substantiator",
                "username" => "admin",
                "password" => "sadffg",
                "charset" => "utf8"
                                    ),
        ),
        "data"=>array(
            "class"=>'\koolreport\datasources\ArrayDataSource',
            "data"=>$_SESSION['TY'],
            "dataFormat"=>"table",
        )
    );
}

The "data" should be inside "dataSources"

function settings()
{
    return array(
        "dataSources"=>array(
            "substantiator" => array(
                "connectionString" => "mysql:host=mysql;dbname=substantiator",
                "username" => "admin",
                "password" => "sadffg",
                "charset" => "utf8"
              ),
            "data"=>array(
                "class"=>'\koolreport\datasources\ArrayDataSource',
                "data"=>$_SESSION['TY'],
                "dataFormat"=>"table",
            )
        ),
    );
}

I feel you are trying to hit & miss in the code. If possible, please spend time to read our documentation or you may copy and modify from our example which is close to your case. The examples will be a great source to avoid this kind of issue.

Richb201 commented on Jan 15, 2021

>>I feel you are trying to hit & miss in the code. If possible, please spend time to read our documentation

EXACTLY! The trial and error method is killing me. There needs to be some documentation. I surely have better things to do than waste my vaulable time asking basic questions up here. Under methods you only show this:

No example. No explanation. Nothing. Perhaps there is a manual that I didn't get?

Under EXAMPLE you show this:

You show this same page under all of these. Are you aware of that?

So to answer your question as to "if I read the documentation?". My answer is "did you"? I am thinking that you need to look at it before accusing paying customers of not looking at it. The same insufficient page appears under all four of the anchors. I can't find an example of the use of "load()" anywhere on your site.

Please show an example of the use of load() as in the "documentation".

KoolReport commented on Jan 15, 2021

Sorry that I did not get what you mean by referring to these examples. Those are correct examples of how to use ArrayDataSource. Two examples, the first one is for associate data and the second one is for table data.

Admitted that we don't have the example for load() method of ArrayDataSource but the description is clear "This allow us to load an array in the setup() function of KoolReport. This load() function support both type of table format "associate" and "table". So you want to use the load() function, here is the example:

<?php
class MyReport extends \koolreport\KoolReport
{
    public function settings()
    {
        return array(
            "dataSources"=>array(
                "array_example_datasource"=>array(
                    "class"=>'\koolreport\datasources\ArrayDataSource',
                ),
            )
        );
    }

    public function setup()
    {
        $this->src("array_example_datasource")
                ->load(array(
                        array("customerName"=>"Johny Deep","dollar_sales"=>100),
                        array("customerName"=>"Angelina Jolie","dollar_sales"=>200),
                        array("customerName"=>"Brad Pitt","dollar_sales"=>200),
                        array("customerName"=>"Nocole Kidman","dollar_sales"=>100),
                 ))
                ->pipe(...)
                ...
                ->pipe($this->dataSource("result"));
    }
}

The documentation may miss some examples but it is correct (I double checked). I am sorry if I made you feel bad, I just want to help only :)

Richb201 commented on Jan 15, 2021

Sorry I got excited but I have limited time to solve this. Since load() is the only method shown. I need an example of how to use the only method shown. I already have an array. It is in a session variable call TY. Can I assume that this would work? I will move the following code to setup()

 $this->src("array_example_datasource")
                ->load(array(
                        $_session['TY'], 'associate' ),
                 ))

And then for the BSelect which will appear right after the above datasource

BSelect::create(array(
    'name'=>"my_select",
    "placeholder"=>"Select TY",
    "data"=>' array_example_datasource'
    )

));

and I modified settings: function settings()

{
    $CI = & get_instance();
    $CI->load->model('MyModel')->library('session');
    return array(
        "dataSources"=>array(
            "substantiator" => array(
                "connectionString" => "mysql:host=mysql;dbname=substantiator",
                "username" => "admin",
                "password" => "sadfsf3fg",
                "charset" => "utf8"
                                    ),
        ),
        "array_example_datasource"=>array(
            "class"=>'\koolreport\datasources\ArrayDataSource',
            "dataFormat"=>"associate",
            "data"=>array($_SESSION['TY'])
        ),
    );
}

Let's forget about the default and bindParamsToInputs. Life is too short for that!

KoolReport commented on Jan 15, 2021

By the way, the links you see in your captured image below is the menu:

So that you can see the whole structure of documentation page. Click to the link, you will go to the section of the page. All the pages in documentation will follow this structure. This is just a way for quick access. You can see it in many websites like well-known Laravel framework documentation, for example Laravel Routing,

Hope that helps.

KoolReport commented on Jan 15, 2021

All 3 parts of your code have problems:

1 . For loading function, it should be:

 $this->src("array_example_datasource")
                ->load($_session['TY'], 'associate')

2 . For BSelect, "data" should be an array, not string. Here is the document

3 . Insider the settings(), I have corrected you earlier that you should put source inside "dataSources" property. Refer to this post.

Also, I guess the $_SESSION['TY'] is array already so you do not need to use array($_SESSION['TY'])

So it should be:

function settings()
{
    $CI = & get_instance();
    $CI->load->model('MyModel')->library('session');
    return array(
        "dataSources"=>array(
            "substantiator" => array(
                "connectionString" => "mysql:host=mysql;dbname=substantiator",
                "username" => "admin",
                "password" => "sadfsf3fg",
                "charset" => "utf8"
            ),
            "array_example_datasource"=>array(
                "class"=>'\koolreport\datasources\ArrayDataSource',
                "dataFormat"=>"associate",
                "data"=>$_SESSION['TY']
            ),
        ),
    );
}

My suggestion: Why don't you just put data directly to BSelect like this, simple and easy, we dont need to use ArrayDataSource.

BSelect::create(array(
    'name'=>"my_select",
    "placeholder"=>"Select TY",
    "data"=>$_SESSION['TY'],
));
Richb201 commented on Jan 16, 2021

Thanks. I like your suggestion under My suggestion. In fact I sent you that exact code above. See The one that says "trial and error" method? That function, getTaxyear() in the code, returns the array, TY. But now I am now loading that array into the $_session['TY'], before getting to the report itself, now.

So, other than including this directly in the view:

BSelect::create(array(

'name'=>"my_select",
"placeholder"=>"Select TY",
"data"=>$_SESSION['TY'],

));

There is NOTHING ELSE I need to do? Right. Please confirm. Nothing . No settings, no default, no bind? Nothing, right? I need you to confirm this before I waste another week on this BS.

KoolReport commented on Jan 16, 2021

Yes, you do not need to do anything else, the $_SESSION["TY"] is available to be accessed from the BSelect.

Richb201 commented on Jan 16, 2021

I have another question. I want to centralize my setup() and settings() in my model file. But the way it works they are called by render(). Can I create the datastores external to the report? Any examples?

KoolReport commented on Jan 16, 2021

Can you further explain your problem and need. May be some pseudo code would help.

Richb201 commented on Jan 16, 2021

Ok. I have 3 separate reports I am building per user, with koolreport. One is a dashboard, one is a form, and one is a full report. Each one, when a report is displayed, calls its own settings(). That means that a new handle to mysql is being opened each time a user wants to run a report and this could happen numerous times in a single user session. Each time a user runs a report for a different tax year, the render runs, and this runs settings and thus opens more handles. I'd like to open the handle once in my php app, and store the handle in a session variable, once per user. When any of the 3 reports is run, it will use the "shared" handle, just like in a file server. Once the user logs off I will close the handle. Opening files is very cpu intensive.

I'd also like to build the datastores once per user. The reason is that due to the need to call render, setup is run every time a user wants to run a report for a different tax year. Very time consuming.

So what I would like is to be able open each datastore just one time (per user) and then close the handles when the user logs off. Has anyone had this question before?

KoolReport commented on Jan 17, 2021

To create many reports sharing same settings(), you can do as following tutorial of How to create many reports sharing the same datasource settings

Richb201 commented on Jan 17, 2021

Great, I will give that a try. How about the setup()? Is there a way that I can run that once in basereport and then just use the datastores it creates throughout the session?

Richb201 commented on Jan 17, 2021

Back to my original issue. I put this in the view: <form method="post">

<?php
BSelect::create(array(
    'name'=>"my_select",
    "placeholder"=>"Select TY",
    "data"=>$_SESSION['TY'],
));
?>
<button type="submit">Submit</button>

</form>

The pull down appears and the first value in the array appears but does not pull down.

KoolReport commented on Jan 18, 2021

I see, I guess the $_SESSION["TY"] data is not in the format "name"=>"value" like it is required by "data" property. Let try this:

<?php
BSelect::create(array(
    'name'=>"my_select",
    "placeholder"=>"Select TY",
    "dataSource"=>$_SESSION['TY'],
    "dataBind"=>array(
        "text"=>"data field that appear as text",
        "value"=>"data field that appear as value",
    ),
));
?>

Please fill in the correct data field name for "text" and "value"

Richb201 commented on Jan 18, 2021

Yes. The array TY is defined as $TY = array('2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023', '2024', '2025', '2026', '2027', '2028', '2029', '2030');

Thus it just has an index and a year. It does not have "text".

KoolReport commented on Jan 18, 2021

I see, I did not know about TY data format. If it is in this format you can be back to the old one:

\koolreport\inputs\BSelect::create(array(
    'name'=>"my_select",
    "placeholder"=>"Select TY",
    "data"=>$_SESSION["TY"],
));

Previously , you said that BSelect does not shown anything, if it is, it should be that your $_SESSION["TY"] is empty array. You can try the below one, it will work:

\koolreport\inputs\BSelect::create(array(
    'name'=>"my_select",
    "placeholder"=>"Select TY",
    "data"=>array('2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023', '2024', '2025', '2026', '2027', '2028', '2029', '2030'),
));
Richb201 commented on Jan 19, 2021

I tried it with

\koolreport\inputs\BSelect::create(array(
'name'=>"my_select",
"placeholder"=>"Select TY",
    "data"=>array('2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023', '2024', '2025', '2026', '2027', '2028', '2029', '2030'),
));

It still doesn't open. Is there any parameter that could be randomly set somewhere that is inhibiting it to open? Can I step through it with the debugger?

KoolReport commented on Jan 19, 2021

I was testing this on our example and see that it worked. Could you please try to open Inspector tools of browser and then click to Bselect and see if there is any js error appeared.

Richb201 commented on Jan 19, 2021

here is a screenshot of inspect. First I notice that multiselect is chosen. I am trying for a single select. You can see the TY's displayed. In running the debugger I see that the class BSelect was not found. I am not sure of the implications of that.

It must be something else. I copied the exact code from the example below and the pull down will also not work. BSelect::create(array(

'name'=>"my_select",
"placeholder"=>"Select customer",
"data"=>array(
    "John Doe"=>"1",
    "Jane Doe"=>"2",
    "Whatever Doe"=>"3",
)    

));

Are there any other controls that come with the KR package that will allow the user to pick one out of a choice of inputs?

Richb201 commented on Jan 19, 2021

I got it working. I am using this instead: \koolreport\inputs\Select::create(array(

"name"=>"customer",
"data"=>$_SESSION['TY'],

));

I am figuring that the documentation that says to use an array for BSelect is not correct. You meant an "associative array"!! How many wasted hours on "trial and error"?

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

Inputs