Filter
Introduction #
Filter
process helps to filter your data based on condition. It is like the WHERE statement in SQL Query. If your data is from database, we encourage you to use the filtering feature of database. However, if your data coming from other sources such as CSV or Microsoft Excel then you need Filter
.
Sample Code #
<?php
use \koolreport\processes\Filter;
class MyReport extends \koolreport\KoolReport
{
public function setup()
{
...
->pipe(new Filter(array(
array("region","=","asia"),
array("customerAge",">",50)
)))
...
}
}
Array condition #
In its standard form, a filter condition is an array of 3 or 4 elements. The first one is a field name, the second one is an operator, the third and forth ones are comparison values. A condition result is calculated by applying a data row's field value on its operator and comparison values.
Operators #
Name | description | example |
---|---|---|
= | Equal to | array("age","=",32) |
!= | Not equal to | array("age","!=",32) |
> | Greater than | array("age",">",32) |
< | Less than | array("age","<",32) |
>= | Greater than or equal to | array("age",">=",32) |
<= | Less than or equal to | array("age","<=",32) |
contain | Contain a string | array("name","contain","John") |
notContain | Not contain a string | array("name","notContain","John") |
startWith | Start with a string | array("name","startWith","John") |
notStartWith | Not start with a string | array("name","notStartWith","John") |
endWith | End with a string | array("name","endWith","John") |
notEndWith | Not end with a string | array("name","notEndWith","John") |
between | Between two given values, not including the end points | array("age","between",24,32) i.e 24 < $row["age"] < 32 |
notBetween | Not between two given values, including the end points | array("age","notBetween",24,32) i.e $row["age"] <= 24 or 32 <= $row["age"] |
betweenInclusive | Between two given values, including the end points | array("age","betweenInclusive",24,32) i.e 24 <= $row["age"] <= 32 |
notBetweenInclusive | Not between two given values, not including the end points | array("age","notBetweenInclusive",24,32) i.e $row["age"] < 24 or 32 < $row["age"] |
in | Value is in an array | array("name","in",array("peter","marry")) |
notIn | Value is NOT in an array | array("name","notIn",array("peter","marry")) |
Logic Operators #
When using with multiple conditions, Filter
process supports and
, or
, xor
logic operators among those conditions:
->pipe(new Filter(array(
array("region","=","asia"),
"or", // "and", "xor"
array("customerAge",">",50)
)))
If not explicitly defined, the default logic is and
. For example, the following Filter
processes are the same:
->pipe(new Filter(array(
array("region","=","asia"),
array("customerAge",">",50)
)))
->pipe(new Filter(array(
array("region","=","asia"),
"and",
array("customerAge",">",50)
)))
Function condition #
Beside conditions in array form, users could use function condition for more flexible filters:
->pipe(new Filter(array(
function ($row) { return ...; }, // return true to keep the row, otherwise to filter it out
function ($row) { return ...; }
)))
Brackets #
For even more complex filtering, round brackets could be used with multiple conditions like in mathematic formula:
->pipe(new Filter(array(
array("income",">","50000"),
"(",
array("income","<","70000"),
"or",
array("income","<","90000"),
")"
)))
Get started with KoolReport
KoolReport will help you to construct good php data report by gathering your data from multiple sources, transforming them into valuable insights, and finally visualizing them in stunning charts and graphs.