CSV Footer

How to use footers and aggregates when exporting to CSV

customerNameproductNameproductLinedollar_sales
Vitachrome Inc. 1937 Lincoln Berline Vintage Cars 3,726
Vitachrome Inc. 1936 Mercedes-Benz 500K Special Roadster Vintage Cars 1,768
Baane Mini Imports 1952 Alpine Renault 1300 Classic Cars 5,572
Baane Mini Imports 1962 LanciaA Delta 16V Classic Cars 5,026
Baane Mini Imports 1958 Setra Bus Trucks and Buses 3,284
Baane Mini Imports 1940 Ford Pickup Truck Trucks and Buses 3,308
Baane Mini Imports 1926 Ford Fire Engine Trucks and Buses 1,283
Baane Mini Imports 1913 Ford Model T Speedster Vintage Cars 2,489
Baane Mini Imports 1934 Ford V8 Coupe Vintage Cars 2,164
Baane Mini Imports 18th Century Vintage Horse Carriage Vintage Cars 2,173
Baane Mini Imports 1917 Maxwell Touring Car Vintage Cars 3,970
Baane Mini Imports 1940s Ford truck Trucks and Buses 3,531
Baane Mini Imports 1939 Cadillac Limousine Vintage Cars 1,671
Baane Mini Imports 1962 Volkswagen Microbus Trucks and Buses 3,864
Baane Mini Imports 1936 Chrysler Airflow Vintage Cars 2,216
Baane Mini Imports 1980’s GM Manhattan Express Trucks and Buses 2,866
Baane Mini Imports 1996 Peterbilt 379 Stake Bed with Outrigger Trucks and Buses 2,851
Baane Mini Imports 1982 Camaro Z28 Classic Cars 3,951
Euro+ Shopping Channel 1969 Corvair Monza Classic Cars 4,469
Euro+ Shopping Channel 1957 Chevy Pickup Trucks and Buses 4,567
Euro+ Shopping Channel 1998 Chrysler Plymouth Prowler Classic Cars 3,262
Euro+ Shopping Channel 1964 Mercedes Tour Bus Trucks and Buses 3,559
Euro+ Shopping Channel 1992 Ferrari 360 Spider red Classic Cars 3,817
Euro+ Shopping Channel 1970 Triumph Spitfire Classic Cars 4,530
Euro+ Shopping Channel 1970 Dodge Coronet Classic Cars 1,821
Euro+ Shopping Channel 1958 Chevy Corvette Limited Edition Classic Cars 1,338
Euro+ Shopping Channel 1992 Porsche Cayenne Turbo Silver Classic Cars 2,768
Euro+ Shopping Channel 1954 Greyhound Scenicruiser Trucks and Buses 1,818
Euro+ Shopping Channel 1950's Chicago Surface Lines Streetcar Trains 2,771
Euro+ Shopping Channel Diamond T620 Semi-Skirted Tanker Trucks and Buses 3,781
Euro+ Shopping Channel 1962 City of Detroit Streetcar Trains 1,706
Danish Wholesale Imports 1972 Alfa Romeo GTA Classic Cars 6,392
Danish Wholesale Imports 2001 Ferrari Enzo Classic Cars 8,435
Danish Wholesale Imports 1969 Ford Falcon Classic Cars 4,115
Danish Wholesale Imports 1903 Ford Model A Vintage Cars 3,005
Danish Wholesale Imports Collectable Wooden Train Trains 3,334
Danish Wholesale Imports 1904 Buick Runabout Vintage Cars 3,095
Danish Wholesale Imports 18th century schooner Ships 5,073
Danish Wholesale Imports 1912 Ford Model T Delivery Wagon Vintage Cars 3,232
Danish Wholesale Imports 1940 Ford Delivery Sedan Vintage Cars 3,774
Danish Wholesale Imports The Schooner Bluenose Ships 2,214
Danish Wholesale Imports The Mayflower Ships 2,512
Danish Wholesale Imports The USS Constitution Ship Ships 1,882
Danish Wholesale Imports The Titanic Ships 3,594
Danish Wholesale Imports The Queen Mary Ships 2,185
Danish Wholesale Imports Pont Yacht Ships 1,119
Rovelli Gifts 1980s Black Hawk Helicopter Planes 4,825
Rovelli Gifts P-51-D Mustang Planes 2,757
Rovelli Gifts 1999 Yamaha Speed Boat Ships 3,315
Rovelli Gifts 1941 Chevrolet Special Deluxe Cabriolet Vintage Cars 3,863
Total: 164,640

This example demonstrates how to use footers and aggregates when exporting to CSV.

$report->run()->exportToCSV(
    array(
        "dataStores" => array(
            "orders" => [
                "separator" => ";",
                "columns" => array(
                    "customerName",
                    "productName",
                    "productLine",
                    "dollar_sales" => [
                        "footer" => "sum",
                        "footerFormat" => function($value, $colMeta) {
                            return number_format($value, 2);
                        },
                        "footerFormat" => [
                            "decimals" => 2,
                            "thousandSeparator" => ",",
                            "decimalPoint" => "."
                        ],
                        "footerText" => "Total: @value || Count: @countSales || Avg: @avgSales",
                    ]
                ),
                "aggregates" => [
                    "countSales" => [
                        "count", 
                        "dollar_sales",
                        "format" => function($value, $colMeta) {
                            return number_format($value, 0, ".", ",");
                        }
                    ],
                    "avgSales" => [
                        "operator" => "avg", 
                        "field" => "dollar_sales",
                        "format" => [
                            "decimals" => 2,
                            "thousandSeparator" => ",",
                            "decimalPoint" => "."
                        ]
                    ],
                ],
            ],
        ),
    ),
)
    ->toBrowser("orders.csv");
<?php
require_once "MyReport.php";

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

use \koolreport\processes\Map;
use \koolreport\processes\Limit;
use \koolreport\processes\ColumnMeta;
use \koolreport\cube\processes\Cube;
use \koolreport\pivot\processes\Pivot;

class MyReport extends koolreport\KoolReport
{
    use \koolreport\excel\CSVExportable;

    function settings()
    {
        return array(
            "dataSources" => array(
                "dollarsales" => array(
                    'filePath' => '../../../databases/customer_product_dollarsales2.csv',
                    'fieldSeparator' => ';',
                    'class' => "\koolreport\datasources\CSVDataSource"
                ),
            )
        );
    }
    function setup()
    {
        $node = $this->src('dollarsales')
        ->pipe(new ColumnMeta(array(
            "dollar_sales" => [
                "type" => "number"
            ],
            "orderDate" => [
                "type" => "datetime"
            ]
        )));
        
        $node->pipe(new Limit(array(
            50, 0
        )))
        ->pipe($this->dataStore('orders'));
    }
}
<?php
use \koolreport\pivot\widgets\PivotTable;
use \koolreport\widgets\koolphp\Table;
?>
<div class="report-content">
	<div style='text-align: center;margin-bottom:30px;'>
        <h1>CSV Footer</h1>
        <p class="lead">How to use footers and aggregates when exporting to CSV</p>
		<form method="post">
			<button type="submit" class="btn btn-primary" formaction="export.php">Export to CSV</button>
		</form>
	</div>
	<div class='box-container'>
		<div>
			<?php
			Table::create(array(
				"dataSource" => $this->dataStore('orders'),
				"columns"=>array(
					"customerName",
					"productName",
					"productLine",
					// "orderDate",
					// "orderMonth",
					// "orderYear",
					// "orderQuarter",
					"dollar_sales" => [
						"footer" => "sum",
						"footerText" => "Total: @value"
					]
				),
				"showFooter" => true,
				"paging"=>array(
					"pageSize"=>5
				)
			));
			?>
		</div>
	</div>
</div>
<?php
include "MyReport.php";
$report = new MyReport();
$report->run()->exportToCSV(
    array(
        "dataStores" => array(
            "orders" => [
                "separator" => ";",
                "columns" => array(
                    "customerName",
                    "productName",
                    "productLine",
                    "dollar_sales" => [
                        "footer" => "sum",
                        "footerFormat" => function($value, $colMeta) {
                            return number_format($value, 2);
                        },
                        "footerFormat" => [
                            "decimals" => 2,
                            "thousandSeparator" => ",",
                            "decimalPoint" => "."
                        ],
                        "footerText" => "Total: @value || Count: @countSales || Avg: @avgSales",
                    ]
                ),
                "aggregates" => [
                    "countSales" => [
                        "count", 
                        "dollar_sales",
                        "format" => function($value, $colMeta) {
                            return number_format($value, 0, ".", ",");
                        }
                    ],
                    "avgSales" => [
                        "operator" => "avg", 
                        "field" => "dollar_sales",
                        "format" => [
                            "decimals" => 2,
                            "thousandSeparator" => ",",
                            "decimalPoint" => "."
                        ]
                    ],
                ],
            ],
        ),
    ),
)
    ->toBrowser("orders.csv");
customerNameproductNameproductLineorderDateorderDayorderMonthorderYearorderQuarterdollar_sales
Vitachrome Inc. 1937 Lincoln Berline Vintage Cars 2003-01-10 00:00:00 10 1 2003 1 3726.45
Vitachrome Inc. 1936 Mercedes-Benz 500K Special Roadster Vintage Cars 2003-01-10 00:00:00 10 1 2003 1 1768.33
Baane Mini Imports 1952 Alpine Renault 1300 Classic Cars 2003-01-29 00:00:00 29 1 2003 1 5571.8
Baane Mini Imports 1962 LanciaA Delta 16V Classic Cars 2003-01-29 00:00:00 29 1 2003 1 5026.14
Baane Mini Imports 1958 Setra Bus Trucks and Buses 2003-01-29 00:00:00 29 1 2003 1 3284.28
Baane Mini Imports 1940 Ford Pickup Truck Trucks and Buses 2003-01-29 00:00:00 29 1 2003 1 3307.5
Baane Mini Imports 1926 Ford Fire Engine Trucks and Buses 2003-01-29 00:00:00 29 1 2003 1 1283.48
Baane Mini Imports 1913 Ford Model T Speedster Vintage Cars 2003-01-29 00:00:00 29 1 2003 1 2489.13
Baane Mini Imports 1934 Ford V8 Coupe Vintage Cars 2003-01-29 00:00:00 29 1 2003 1 2164.4
Baane Mini Imports 18th Century Vintage Horse Carriage Vintage Cars 2003-01-29 00:00:00 29 1 2003 1 2173

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