KoolReport's Forum

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

Input does'nt work #258

Open arthur opened this topic on on Apr 24, 2018 - 14 comments

arthur commented on Apr 24, 2018

Hi,

I want to filter a Table by Date so I use the input package. However something must be wrong because it doesn’t work… Here is my code :

Main file

class Commercial extends \koolreport\KoolReport
{

  use \koolreport\inputs\Bindable;
    use \koolreport\inputs\POSTBinding;

function defaultParamValues()
    {
        return array(
            "thedate"=>0,
        );
    }

    function bindParamsToInputs()
    {
        return array(
            "Date",
        );
    }

    function settings()
    {
        return array(
          "dataSources"=>array(
            "sales"=>array(
                "class"=>'\koolreport\excel\ExcelDataSource',
                "filePath"=>"./databases/commandes.xlsx",
                "charset"=>"utf8",
                "firstRowData"=>false,
                "sheetName"=>"TDV_18",
                "sheetIndex"=>0,
            ),        
        )
      );
    }

    function setup()
    {
$this->src('sales')
->pipe(new Group(array(
            "by"=>"Date",
        )))
        ->pipe($this->dataStore('date'));

$this->src('sales')
        ->pipe(new Filter(array(
            array("Ccial","=",$_SESSION['Username']),
            array("Ccial","!=",null),
            array("Société","!=",null),
        )))
         ->pipe(new Filter(array(
          "or",
        array("Date","=","JANVIER"),
           array("Date","=","FÉVRIER"),
           array("Date","=","MARS"),
        )))
        ->pipe($this->dataStore('commandes'));
    }
}

And here is my view file code

$customerName = "";

    while($row = $this->dataStore("customers")->pop())
    {
        if($row["thedate"]==$this->params["thedate"])
        {
            $customerName =$row["Date"];
        }
    }

?>

<div class="text-center">
    <h1>Sales Report</h1>
    <h4>This report shows top 10 sales by customer</h4>
   <form method="post">
                <div class="text-center">
                    <h1>List order of a customer</h1>
                    <div class="row form-group">
                        <div class="col-md-6 col-md-offset-3">
                            <?php
                            Select::create(array(
                                "name"=>"thedate",
                                "data"=>array(
        "JANVIER"=>"1",
        "FEVRIER"=>"2",
        "MARS"=>"3",
    ),
                                "attributes"=>array(
                                    "class"=>"form-control"
                                )
                            ));
                            ?>
                        </div>
                    </div>
                    <div class="form-group">
                        <button class="btn btn-primary">Ok</button>
                    </div>
                </div>
            </form>
</div>
<hr/>

<?php
Table::create(array(
    "dataStore"=>$this->dataStore('commandes'),
        "columns"=>array(
            "Date"=>array(
                "label"=>"Date"
            ),
            "Ccial"=>array(
                "label"=>"Customer"
            ),
            "Société"=>array(
                "type"=>"string",
                "label"=>"Amount",
            )
        ),
  "cssClass"=>array(
        "table"=>"table table-hover table-bordered"
    )
));

Thanks a lot !

KoolReport commented on Apr 24, 2018

In this function

    function bindParamsToInputs()
    {
        return array(
            "Date",
        );
    }

it should be:

    function bindParamsToInputs()
    {
        return array(
            "thedate",
        );
    }

This actually the short form of

    ...
    "thedate"=>"thedate"
    ...

It binds the parameters name "thedate" to the Select input named "thedate"

arthur commented on Apr 24, 2018

Thanks but it does'nt work either. It's loading but nothing happens...

KoolReport commented on Apr 24, 2018

Reading again your code, I see that after user select JANVIER, FEVRIER or MARS, the form will post back and you will received parameters $this->params['thedate'] with value 1, 2 or 3 respectively. However I see no where you use this parameter for filtering result.

arthur commented on Apr 24, 2018

That's something I didnt understand in the documentation : where should I place this to filter ?

KoolReport commented on Apr 24, 2018

This is not related to your code, it is just the basic for filtering:

...
->pipe(new Filter(array(
    array("name","=",$this->params["name"])
)))

Suppose your data has column "name" and you want to get only those rows with a selected name ( from dropdown for example). Now when user select a name and postback, the selected name will be stored in $this->params["name"]. So as you see from above code, data running through Filter process, only those rows which has name = selected name will be passed ... may be to dataStore() for later displayed on table.

arthur commented on Apr 24, 2018

Ok, got it. Now the filter work. Is it possible, by defaut, not to apply a filter and display all the rows ?

KoolReport commented on Apr 24, 2018

Then you remove all filters

arthur commented on Apr 24, 2018

Yes but I must have the variable filter :

$this->src('sales')

        ->pipe(new Filter(array(
            array("Ccial","=",$_SESSION['Username']),
            array("Ccial","!=",null),
            array("Société","!=",null),
        )))

         ->pipe(new Filter(array(
              array("Date","=",$this->params["thedate"]),
            
        )))

         
        ->pipe($this->dataStore('commandes'));

The other filter is necessary because it allows each salesman to see its sales. But when he connects to his session, I want him to see the full list, and then filter if he wants to. Am I clear enough ?

arthur commented on Apr 25, 2018

And just a last question for this part. If I'm using the checkbox list, I'll have an array in return. To use this array inside my filter I'm trying this

->pipe(new Filter(array(

            foreach ($this->params['thedate'] as $date) {
   array("Date","=",$date),
}
           
        )))

But I have a 500 error returning...

KoolReport commented on Apr 25, 2018

You do this:

$filter_args = array("or");
foreach($this->params["thedate"] as $date)
{
    array_push($filter_args,array(
        "Date","=",$date
    ));
}

$this->src('sales')
->pipe(..)
...
->pipe(new Filter($filter_args))

arthur commented on Apr 25, 2018

Still got error 500... Here is the full code :


     $filter_args = array("or");
foreach($this->params["thedate"] as $date)
{
    array_push($filter_args,array(
        "Date","=",$date
    ));
}   

$this->src('sales')

        ->pipe(new Filter(array(
            array("Ccial","=",$_SESSION['Username']),
            array("Ccial","!=",null),
            array("Société","!=",null),
        )))

        ->pipe(new Filter($filter_args))

        
        ->pipe($this->dataStore('commandes'));
KoolReport commented on Apr 25, 2018

Could you please put error_reporting(E_ALL); on top of index.php. This will allow PHP to output error detail instead of just general 500.

KoolReport commented on Apr 25, 2018

By the way, if you have change the thedate to ChexkBoxList, you should do this as well:

function defaultParamValues()
    {
        return array(
            "thedate"=>array(),
        );
    }

Default value of thedate should be empty array(). Or any pre-selected value that you want.

arthur commented on Apr 25, 2018

It works!!! Thanks a lot.

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