Aggregated Methods

Learn about DataStore's aggregated methods

Original data
nameageincome
John 26 $50,000
Marry 29 $60,000
Peter 34 $100,000
Donald 28 $80,000
filter()

$store = $this->dataStore("data")
        ->filter("income",">",50000);
nameageincome
Marry 29 $60,000
Peter 34 $100,000
Donald 28 $80,000
filter($callback)

$store = $this->dataStore("data")
        ->filter(function($row){
            return $row["income"]>50000;
        });
nameageincome
Marry 29 $60,000
Peter 34 $100,000
Donald 28 $80,000
where()

$store = $this->dataStore("data")
        ->where("income",60000);
nameageincome
Marry 29 $60,000
except()

$store = $this->dataStore("data")
        ->except("income","age");
name
John
Marry
Peter
Donald
only()

$store = $this->dataStore("data")
        ->only("name","income");
nameincome
John $50,000
Marry $60,000
Peter $100,000
Donald $80,000
whereIn()

$store = $this->dataStore("data")
        ->whereIn("name",["John","Marry"]);
nameageincome
John 26 $50,000
Marry 29 $60,000
whereNotIn()

$store = $this->dataStore("data")
        ->whereNotIn("name",["John","Marry"]);
nameageincome
Peter 34 $100,000
Donald 28 $80,000

This example shows some of most used methods for filtering in DataStore.

Beside above most used methods, there are number of additional methods:

  1. all(): Return all rows
  2. top($size,$offset): Get the top {$size} rows starting from {$offset}
  3. topByPercent($percent): Get top percent of rows
  4. bottom($size): Get the bottom {$size} rows
  5. bottomByPercent($percent): Get bottom percent of rows
  6. first($callback): Return the first row matching condition defined by callback function
  7. last($callback): Return the last row matching condition defined by callback function
  8. take($limit): Get number of rows, if $limit is negative, take the bottom
<?php
require_once "MyReport.php";

$report = new MyReport;
$report->run()->render();
<?php
//Step 1: Load KoolReport
require_once "../../../load.koolreport.php";

use \koolreport\processes\ColumnMeta;
use \koolreport\processes\Filter;

//Step 2: Creating Report class
class MyReport extends \koolreport\KoolReport
{
    protected function settings()
    {
        return array(
            "dataSources"=>array(
                "data"=>array(
                    "class"=>'\koolreport\datasources\ArrayDataSource',
                    "dataFormat"=>"table",
                    "data"=>array(
                        array("name","age","income"),
                        array("John",26,50000),
                        array("Marry",29,60000),
                        array("Peter",34,100000),
                        array("Donald",28,80000),
                    )
                )
            )
        );
    }
    protected function setup()
    {
        //Prepare data
        $this->src("data")
        ->pipe(new ColumnMeta(array(
            "income"=>array(
                "type"=>"number",
                "prefix"=>"$"
            )
        )))
        ->pipe($this->dataStore("data"));
    }
}
<?php
    use \koolreport\widgets\koolphp\Table;
    use \koolreport\widgets\google\LineChart;
?>
<div class="report-content">
    <div class="text-center">
        <h1>Aggregated Methods</h1>
        <p class="lead">Learn about DataStore's aggregated methods</p>
    </div>
    
    <h5>Original data</h5>
    <?php
    Table::create(array(
        "dataSource"=>$this->dataStore("data"),
        "cssClass"=>array(
            "table"=>"table-bordered table-striped table-hover"
        )
    ));
    ?>

    <div class="row">
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                <b>filter()</b>
                </div>
                <div class="card-body">
<pre style="font-weight:bold"><code>
$store = $this->dataStore("data")
        ->filter("income",">",50000);
</code></pre>
                    <?php
                    Table::create(array(
                        "dataSource"=>$this->dataStore("data")->filter("income",">",50000),
                        "cssClass"=>array(
                            "table"=>"table-bordered table-striped table-hover"
                        )
                    ));
                    ?>
                </div>
            </div>        
        </div>
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                <b>filter($callback)</b>
                </div>
                <div class="card-body">
<pre style="font-weight:bold"><code>
$store = $this->dataStore("data")
        ->filter(function($row){
            return $row["income"]>50000;
        });
</code></pre>
                    <?php
                    Table::create(array(
                        "dataSource"=>$this->dataStore("data")->filter(function($row){
                            return $row["income"]>50000;
                        }),
                        "cssClass"=>array(
                            "table"=>"table-bordered table-striped table-hover"
                        )
                    ));
                    ?>
                </div>
            </div>        
        </div>

        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                <b>where()</b>
                </div>
                <div class="card-body">
<pre style="font-weight:bold"><code>
$store = $this->dataStore("data")
        ->where("income",60000);
</code></pre>
                    <?php
                    Table::create(array(
                        "dataSource"=>$this->dataStore("data")->where("income",60000),
                        "cssClass"=>array(
                            "table"=>"table-bordered table-striped table-hover"
                        )
                    ));
                    ?>
                </div>
            </div>        
        </div>
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                <b>except()</b>
                </div>
                <div class="card-body">
<pre style="font-weight:bold"><code>
$store = $this->dataStore("data")
        ->except("income","age");
</code></pre>
                    <?php
                    Table::create(array(
                        "dataSource"=>$this->dataStore("data")->except("income","age"),
                        "cssClass"=>array(
                            "table"=>"table-bordered table-striped table-hover"
                        )
                    ));
                    ?>
                </div>
            </div>        
        </div>
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                <b>only()</b>
                </div>
                <div class="card-body">
<pre style="font-weight:bold"><code>
$store = $this->dataStore("data")
        ->only("name","income");
</code></pre>
                    <?php
                    Table::create(array(
                        "dataSource"=>$this->dataStore("data")->only("name","income"),
                        "cssClass"=>array(
                            "table"=>"table-bordered table-striped table-hover"
                        )
                    ));
                    ?>
                </div>
            </div>        
        </div>
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                <b>whereIn()</b>
                </div>
                <div class="card-body">
<pre style="font-weight:bold"><code>
$store = $this->dataStore("data")
        ->whereIn("name",["John","Marry"]);
</code></pre>
                    <?php
                    Table::create(array(
                        "dataSource"=>$this->dataStore("data")->whereIn("name",["John","Marry"]),
                        "cssClass"=>array(
                            "table"=>"table-bordered table-striped table-hover"
                        )
                    ));
                    ?>
                </div>
            </div>                
        </div>
        <div class="col-md-6">
            <div class="card">
                <div class="card-header">
                <b>whereNotIn()</b>
                </div>
                <div class="card-body">
<pre style="font-weight:bold"><code>
$store = $this->dataStore("data")
        ->whereNotIn("name",["John","Marry"]);
</code></pre>
                    <?php
                    Table::create(array(
                        "dataSource"=>$this->dataStore("data")->whereNotIn("name",["John","Marry"]),
                        "cssClass"=>array(
                            "table"=>"table-bordered table-striped table-hover"
                        )
                    ));
                    ?>
                </div>
            </div>                        
        </div>
        <div class="col-md-6">
        
        </div>
    </div>

    

</div>

What People Are Saying

"KoolReport helps me very much in creating data report for my corporate! Keep up your good work!"
-- Alain Melsens

"The first use of your product. I was impressed by its easiness and powerfulness. This product is a great and amazing."
-- Dr. Lew Choy Onn

"Fantastic framework for reporting!"
-- Greg Schneider

Download KoolReport Get KoolReport Pro