Table Section Titles

Divide the table in sections with titles

In order to add Sections dividing the content of the table, we need to order the data results in such way that it is in order of the groups that need to be split. For instance, if we have a table for Invoices and we want to divide  the data in status (paid, pending, or canceled), then we need to order the data by status.

Select * from invoices order by status

then we can use the array variable $tableSections, detect will be the value that we are looking for when it comes to grouping, when the value is different that the previous row, that's where the Section Title is inserted. txt_cols is the data used from the database that will be iset to create the text of the Section and section_text is the text format that will be replaced with the values from section_text

//Add Sections to the Table based on data changes
$tableSections = array(
    'detect'=>'status',
    'style' => 'color:red' //optional
    'text_cols' => array('status'),
    'section_text'=>"status Invoices"
);

 

style allows you to give the section a desired CSS styling, the default is: 

 

{
  padding:10px 20px;
  font-weight:900; 
  border-bottom:1px solid #333;
  color:#333;
  background:#CCC
}

 
The previous example generates 3 red sections: paid Invoices, pending invoices, canceled Invoices
 
If we want to use more text in the title with values from the database we simply add more text_cols and use those values in section_text
 
Select *,  
  DATE_FORMAT(invoice_date, "%M %Y") as month_year,
  DATE_FORMAT(invoice_date, "%M") as month, 
  DATE_FORMAT(invoice_date, "%Y") as year  
FROM
  invoices 
ORDER BY
  invoice_date DESC

//Add Sections to the Table based on data changes
$tableSections = array(
    'detect'=>'month_year',
    'style' => 'text-align:center' //optional, this is the default
    'text_cols' => array('month','year'),
    'section_text'=>"(year) - Invoices from month"
);

 

The previous examples creates titles like this:

(2022) - Invoices from October
(2022) - Invoices from November
etc