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 #

Namedescriptionexample
=Equal toarray("age","=",32)
!=Not equal toarray("age","!=",32)
>Greater thanarray("age",">",32)
<Less thanarray("age","<",32)
>=Greater than or equal toarray("age",">=",32)
<=Less than or equal toarray("age","<=",32)
containContain a stringarray("name","contain","John")
notContainNot contain a stringarray("name","notContain","John")
startWithStart with a stringarray("name","startWith","John")
notStartWithNot start with a stringarray("name","notStartWith","John")
endWithEnd with a stringarray("name","endWith","John")
notEndWithNot end with a stringarray("name","notEndWith","John")
betweenBetween two given values, not including the end pointsarray("age","between",24,32) i.e 24 < $row["age"] < 32
notBetweenNot between two given values, including the end pointsarray("age","notBetween",24,32) i.e $row["age"] <= 24 or 32 <= $row["age"]
betweenInclusiveBetween two given values, including the end pointsarray("age","betweenInclusive",24,32) i.e 24 <= $row["age"] <= 32
notBetweenInclusiveNot between two given values, not including the end pointsarray("age","notBetweenInclusive",24,32) i.e $row["age"] < 24 or 32 < $row["age"]
inValue is in an arrayarray("name","in",array("peter","marry"))
notInValue is NOT in an arrayarray("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.