Skip to main content

Quick Start

First, we need to procure the data. For maximum functionality, the result should be cubed. The following is an example SQL query for PostgreSQL.

-- name, country, and month are the dimensions, count and sum are the measures.
-- grouping is used to identify if a dimension is subtotaled.
SELECT name,
country,
month,
COUNT(*) AS count,
SUM(price) AS sum,
GROUPING(name, country, month) AS grouping
FROM items
GROUP BY DISTINCT CUBE(name, country, month)
-- ORDER BY clause determines the order of the dimensions in the output
ORDER BY name,
country,
month

Then, use whatever means to execute the query and fetch the result as an array of array. We can create a pivot table as follows:

use Rekalogika\PivotTable\ArrayTable\ArrayTableFactory;
use Rekalogika\PivotTable\PivotTableTransformer;
use Rekalogika\PivotTable\TableRenderer\BasicTableRenderer;

// Create a data cube from the result set
$cube = ArrayTableFactory::createCube(
input: $data,
// must be in the same order as the GROUPING() function in the SQL query:
dimensionFields: ['name', 'country', 'month'],
measureFields: ['count', 'sum'],
groupingField: 'grouping',
legends: [
'@values' => 'Values',
'name' => 'Item Name',
'country' => 'Country',
'month' => 'Month',
'count' => 'Count',
'sum' => 'Sum',
],
subtotalLabels: [
'name' => 'All Names',
'country' => 'All Countries',
'month' => 'All Months',
],
);

// Transform the cube to HTML table object
$htmlTable = PivotTableTransformer::transform(
cube: $cube,
rows: ['country', 'month'], // Rows
columns: ['@values', 'name'], // Columns
measures: ['count', 'sum'], // Which measures to include
withSubtotal: ['name', 'country'], // Add subtotals for these dimensions
);

// Render to HTML string
$html = BasicTableRenderer::render($htmlTable);
echo $html;

The resulting HTML table will look like this:

CountryMonthCountSum
NameName
itemAitemBitemCAll namesitemAitemBitemCAll names
CN202501415102586.07434.223375.716396
202502null516null2575.55337.292912.84
202503453122823.542849.011488.27160.75
2025043nullnull31301.73nullnull1301.73
202505null3null3null1845.78null1845.78
All months11149346711.347704.565201.219617.1
JP2025011348107.511410.791765.043283.34
202502515113119.07393.392081.025593.48
202503null358null19663656.165622.16
202504352102014.322348.931042.425405.67
202505null112null973.91725.81699.71
All months91317395240.97093.029270.4421604.36
KR202501nullnull22nullnull1134.361134.36
20250253191668.592239.52240.944149.05
202503354121891.441781.152191.785864.37
2025041528855.942641.751274.364772.05
20250552null72231.2458.68null2689.88
All months14159386647.177121.14841.4418609.71
All countries2025015411202693.581845.016275.1110813.7
2025021097264787.665208.462659.2512655.37
20250371312324714.986596.167336.1418647.28
2025047104214171.994990.682316.7811479.45
202505561122231.23278.37725.86235.37
All months34423511118599.4121918.6819313.0859831.17