KoolReport's Forum

Official Support Area, Q&As, Discussions, Suggestions and Bug reports.

Help me on multi filter with csv data source. #857

Open Hassan Fawad opened this topic on on May 8 - 26 comments

Hassan Fawad commented on May 8

How to handle the post date. here is the code

<?php

require_once "../../../../autoload.php";

use \koolreport\KoolReport;
use \koolreport\processes\Filter;
use \koolreport\processes\TimeBucket;
use \koolreport\processes\Group;
use \koolreport\processes\Limit;



class SakilaRental extends KoolReport
{
   
	use \koolreport\inputs\Bindable;
	use \koolreport\inputs\POSTBinding;
	
	 protected function defaultParamValues()
    {
        return array(
            "portalSelect2"=>array(),
            "countrySelect2"=>array(),
            "sizeSelect2"=>array(),
        );
    }
    
    protected function bindParamsToInputs()
    {
        return array(
            
			"Creative"=>"sizeSelect2",
            "Adunit"=>"portalSelect2",
			"Country"=>"countrySelect2",
        );
    }
	
	 public function settings()
    {
        return array(
            "dataSources"=>array(
                "6months"=>array(
                    "class"=>'\koolreport\datasources\CSVDataSource',
                    'filePath'=>dirname(__FILE__)."/Video-benchmarksNEW.csv",
                )
            )
        );
    }   
	   
	   
    protected function setup()
    {
	
		
		$query_params = array();
        if($this->params["sizeSelect2"]!=array())
        {
            $query_params[":Creative"] = $this->params["sizeSelect2"];
        }
        if($this->params["portalSelect2"]!=array())
        {
            $query_params[":Adunit"] = $this->params["portalSelect2"];
        }
        if($this->params["countrySelect2"]!=array())
        {
            $query_params[":Country"] = $this->params["countrySelect2"];
        }
		
	
        $this->src('6months')
		
		
		
		
		  
       
	    ->pipe(new Group(array(
           
			"by"=>"Creative",
            "avg"=>"CTR"
        )))
		
		 ->pipe(new Filter(array(
            array("Creative","contain","v"),
			
			
			
           	
        )))
		
		
		
		
		
		
        ->pipe($this->dataStore('ctr_by_size'));
		
		
		
    } 
}

How to write the filters for 3 inputs?

David Winterburn commented on May 9

Hi Hassan,

Please explain in detail so we understand your question. Thanks!

Hassan Fawad commented on May 9

Hi,

I have set 3 filters on my report. But when I click array is coming empty. and another thing is, I want to know how to handle dynamic filters based on inputs.

3 inputs = 3 multi selects Data source: csv

Hassan Fawad commented on May 14

so, no one will help?

David Winterburn commented on May 14

Hi Hassan,

I would suggest you place the filter process before the group process like this:

->pipe(new Filter(array(
	array("Creative","contain",$query_params[":Creative"]),
	array("field2","contain",$query_params[":field2"]),
	array("field3","contain",$query_params[":field3"]),
)))
->pipe(new Group(array(
	"by"=>"Creative",
	"avg"=>"CTR"
)))

Please try this and let us know if there's any problem. Thanks!

Hassan Fawad commented on May 14

After adding below lines before group. No data is showing up. nothing in table and nothing in filter dropdowns

->pipe(new Filter(array(

array("Creative","contain",$query_params[":Creative"]),
array("Adunit","contain",$query_params[":Adunit"]),
array("Country","contain",$query_params[":Country"]),

)))

David Winterburn commented on May 14

Print out your $query_params to find out their values. Check your array data values as well. And remember by default the filter process use AND operator for multiple filters.

Hassan Fawad commented on May 14

array is coming up empty. why?

Hassan Fawad commented on May 15

hey please, I need quick help on it. but you guys are delaying

Hassan Fawad commented on May 15

below filter is not working, though array is returning value

->pipe(new Filter(array(

array("Creative","=",$query_params[":Creative"]),

)))

print_r($query_params[":Creative"]); output: Array ( [0] => 400x300v )

no data is appearing particular filter

Hassan Fawad commented on May 15

and I need to implement below if conditions for filters.

/*if($query_params[":Creative"]==array() && $query_params[":Adunit"]==array() && $query_params[":Country"]==array())

	{
	
			->pipe(new Filter(array(
	 		array("Creative","contain","v"),
			)))
	}
	
	
	if($query_params[":Creative"]!=array() && $query_params[":Adunit"]==array() && $query_params[":Country"]==array())
	{
	
			->pipe(new Filter(array(
	 		array("Creative","contain","v"),
			array("Creative","=",$query_params[":Creative"]),
			)))
	}
	
	if($query_params[":Creative"]==array() && $query_params[":Adunit"]!=array() && $query_params[":Country"]==array())
	{
	
			->pipe(new Filter(array(
	 		array("Creative","contain","v"),
			array("Adunit","=",$query_params[":Adunit"]),
			)))
	}
	
	if($query_params[":Creative"]==array() && $query_params[":Adunit"]==array() && $query_params[":Country"]!=array())
	{
	
			->pipe(new Filter(array(
	 		array("Creative","contain","v"),
			array("Country","=",$query_params[":Country"]),
			)))
	}
	
	
	if($query_params[":Creative"]!=array() && $query_params[":Adunit"]!=array() && $query_params[":Country"]!=array())
	{
	
			->pipe(new Filter(array(
	 		array("Creative","contain","v"),
			array("Adunit","=",$query_params[":Adunit"]),
			array("Country","=",$query_params[":Country"]),
			
			)))
	}*/
Hassan Fawad commented on May 15

Need to implement same logic with CSV

https://www.koolreport.com/examples/reports/advanced/multiple_data_filters/

David Winterburn commented on May 15

Looks like your param is an array instead of a scalar value:

print_r($query_params[":Creative"]); output: Array ( [0] => 400x300v )

Your data row's "Creative" element is not an array. No wonder the filter returns empty result:

array("Creative","=",$query_params[":Creative"]),

Put the param value (not an array) to the filter. Try one filter first. If it works move on to multi filters.

Hassan Fawad commented on May 15

how to put param value? I need multiselect as in your example with mysql data source

Hassan Fawad commented on May 15

do you want me to give you my csv to get better idea?

Hassan Fawad commented on May 15

here is the code of my 3 dropdowns filters

<?php

                    Select2::create(array(
                        "name"=>"portalSelect2",
                        "multiple"=>true,
                        "dataStore"=>$this->src("6months")
						
						
						
						 ->pipe(new Group(array(
       
						"by"=>"Creative",
						"by"=>"Adunit",
        
    					))),
                        "dataBind"=>"Adunit",
                        "attributes"=>array(
                            "class"=>"form-control",
                        )
                    ));
                    ?>         

<?php

                    Select2::create(array(
                        "name"=>"countrySelect2",
                        "multiple"=>true,
                        "dataStore"=>$this->src("6months")
						
						
						
						 ->pipe(new Group(array(
       
						"by"=>"Creative",
						"by"=>"Country",
        
    					))),
                        "dataBind"=>"Country",
                        "attributes"=>array(
                            "class"=>"form-control",
                        )
                    ));
                    ?>        

<?php

                    Select2::create(array(
                        "name"=>"sizeSelect2",
                        "multiple"=>true,
                        "dataStore"=>$this->src("6months")
						
						->pipe(new Filter(array(
						array("Creative","contain","v"), 
						)))
						
						 ->pipe(new Group(array(
       
						"by"=>"Creative",
        
    					)))
						
						,
                        "dataBind"=>"Creative",
                        "attributes"=>array(
                            "class"=>"form-control",
                        )
                    ));
                    ?>
David Winterburn commented on May 15

You have an array with one element. Don't you know how to get the only element value from your array?

Hassan Fawad commented on May 15

what do you mean by? I could have multiple values from one array. normal php code to get multiple selected values is not working with koolreport framework. check above the code I have sent to you.

Hassan Fawad commented on May 15

so 3 filters = 3 arrays. all 3 arrays could return multiple values.

Hassan Fawad commented on May 15

there?

David Winterburn commented on May 16

Hi Hassan,

In case your parameters is an array from a multiple-value select, don't use the "equals", "=" or "contains" operator in your filter. Please try the "in" operator like this:

->pipe(new Filter(array(
array("Creative","in",$query_params[":Creative"]),
array("Adunit","in",$query_params[":Adunit"]),
array("Country","in",$query_params[":Country"]),
)))

Make sure your $query_params[$key] is not an empty array first.

Hassan Fawad commented on May 16

Thanks for the reply. It worked. An other help. I want below filter by default. and then the three filters based on below filter.

array("Creative","contain","v"),

Hassan Fawad commented on May 16

and I want, if user select value from one filter, it should return. as of now to get correct data, all three filters need to select

David Winterburn commented on May 16

Not sure if I fully understand your question but make a try of this code:

$filers = array();
if (! empty($query_params[":Creative"])) {
    array_push($filters, array("Creative","in",$query_params[":Creative"]));
}
if (! empty($query_params[":Adunit"])) {
    array_push($filters, array("Adunit","in",$query_params[":Adunit"]));
}
if (! empty($query_params[":Country"])) {
    array_push($filters, array("Country","in",$query_params[":Country"]));
}

...
->pipe(new Filter(array(
    array("Creative","contain","v"),
)))
->pipe(new Filter(array(
    $filters
)))
...

Let us know if it works for your case. Thanks!

Hassan Fawad commented 4 days ago

Hi,

Thanks for kind help. I had manage to to solve this issue with some other solution. I have now another issue. I have added one more filter that is date picker.

Default csv contains 6 months data. so by default report shows of 6 months. I need date filter of 30, 90 and custom.

can you write the function or tell in which file I need to write? Another thing every month we will update the csv. so this date filter should automatically update with max and min date according to the date column.

can you help me on this?

Hassan Fawad commented 2 days ago

there?

Hassan Fawad commented 22 hours ago

could you please reply?

Give tips to supporter for his good work

If you feel that supporter has done a good work, consider giving him some credit. Any amount put into the tips box below is appreciated. By doing so, you have contribtuted to the existence of KoolReport and the quality of support.

Tips box
None yet

Inputs