Groups



The group() method allows the report to be split into sections grouped by a specific column value. Each section can have headers and trailers and throw a page before on group using the throwPageBefore() method.

For headers, the header() method will display the selected column at the top of each section instead of in the main report table and the customHeader() method allows you specify a complete block of text to show before each section, position and styled according to the passed css definition.

With trailers, you have column trailers and custom trailers. Column trailers allow a particular report column to be displayed at the end of each group underneath another column to provide, for example, a total or average at the end of a group. In order to show a group sum or other aggregate you need to create a new expression and provide the column to aggregate on (see the example code on this page). Then you can create a trailer and set the column underneath which to place the trailer. The customTrailer() option, like customHeader() allows you to display a text block after each group styled using a css block.



Usage:
 
\Reportico\Engine\Builder\build
()
  ->
group(column :column name to group on)
    ->
throwPageBefore()
    ->
header(column :name of the column to display in the group header)
    ->
customHeader(
        
header text :A block of text which can contain column values using the {} notation,
        
header style :A CSS string or array of CSS style pairs indicating how to style and position the header)
    ->
trailer(column :name of the column to display in the group trailer)
    ->
label(label :The label to display against a column header or trailer)
    ->
below(column :At the bottom of which column display trailer value for example a group sum or average)
    ->
customTrailer(
        
header text :A block of text which can contain column values using the {} notation,
        
header style :A CSS string or array of CSS style pairs indicating how to style and position the trailer)



In this example, we are showing the stock levels of each product grouped by product category. The expression() method is used to create a running count of the stock level grouped by category so that we can show group total under the in stock column

The customTrailer() method is used to create a right adjusted div with a dark background to combine the category and count into a summary box.The column values are included using the {columnname} notation. The customHeader() does the same but places the block at the start of the group.

Run Demo


<?php
      
require_once(__DIR__ .'/../vendor/autoload.php');
      

      
\Reportico\Engine\Builder::build()
          ->
properties([ "bootstrap_preloaded" => true])
          ->
datasource()->database("mysql:host=localhost; dbname=DATABASE NAME")->user("USER")->password("PASSWORD")
          ->
title     ("Product Stock")
          ->
description     ("Produces a list of our employees")
          ->
sql       ("
              SELECT  ProductID id, ProductName product, UnitsInStock in_stock, UnitsOnOrder on_order, companyname Company, country, categoryname category
              FROM northwind_products 
              join northwind_suppliers on northwind_products.supplierid = northwind_suppliers.supplierid
              join northwind_categories on northwind_products.categoryid = northwind_categories.categoryid
              WHERE 1 = 1  
              ORDER BY categoryname
                "
)
          ->
expression("total_stock")->sum("in_stock","category")
          ->
group("category")
              ->
throwPageBefore()
              ->
header("category")
              ->
trailer("total_stock")->below("in_stock")->label("Total")
              ->
customHeader("A custom header with a dotted border for category {category}""border: dotted 1px #000; left: 0px; margin-bottom: 10px; margin-top: 20px; margin-left: auto;  width: 50%; padding: 10px;")
              ->
customTrailer("Total in stock for category {category} is {total_stock}""border: solid 4px #22D; background-color: #222; color: #fff;  right: 0px; margin-left: auto;  width: 50%; padding: 10px;")
              ->
header("category")
          ->
column("in_stock")->justify("right")
          ->
column("on_order")->justify("right")
          ->
execute();