PHP Classes

PHP Report Grouping: Generate HTML tables grouping data records

Recommend this page to a friend!
  Info   View files Example   View files View files (2)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 177 All time: 8,733 This week: 94Up
Version License PHP version Categories
reportgrouping 3GNU General Publi...1HTML, PHP 5, Databases
Description 

Author

This class can generate HTML tables grouping data records.

It can take an array of records generate reporting output. Currently it can:

- Output the report in any other format using callback functions
- Group data by the values of given columns
- Show a subtotal at each break in grouped data values
- Show a grand total

Innovation Award
PHP Programming Innovation award nominee
September 2020
Number 10
Many Web sites display reports of data eventually retrieved from databases or other data sources.

Usually those reports show the data that was retrieved. In some cases, the values that represent values calculated using values of the report itself, like for instance the total of column values.

This class provides a solution to generate this kind of report that can be used to display data from other types of reports, like any type of data source that can be accessed to retrieve data into arrays.

Manuel Lemos
Picture of Steven Hoyt
  Performance   Level  
Name: Steven Hoyt <contact>
Classes: 3 packages by
Country: United States United States
Age: ???
All time rank: 3488466 in United States United States
Week rank: 312 Up38 in United States United States Up
Innovation award
Innovation award
Nominee: 2x

Example

<?php
//////////////////////////////////////////////////////////
// initialization settings
ini_set('display_errors' , 'on' );
ini_set('error_reporting' , E_ALL & ~ (E_NOTICE | E_WARNING) );
ini_set('error_reporting' , E_ALL & ~E_NOTICE );
ini_set('max_execution_time' , 0 );
ini_set('memory_limit' , -1 );
//////////////////////////////////////////////////////////
// required files
require_once 'ReportGrouping.php';
//////////////////////////////////////////////////////////
// main script
$records = [];
$records[] = [
                           
'FIRST' => 'Johnny' ,
                           
'LAST' => 'Rocket' ,
                           
'EMAIL' => 'jr@example.com' ,
                           
'AGE' => 32 ,
                           
'SALARY' => 45000
                         
];
$records[] = [
                           
'FIRST' => 'Connie' ,
                           
'LAST' => 'Rocket' ,
                           
'EMAIL' => 'cr@example.com' ,
                           
'AGE' => 22 ,
                           
'SALARY' => 55000
                         
];
$records[] = [
                           
'FIRST' => 'Lonnie' ,
                           
'LAST' => 'Rocket' ,
                           
'EMAIL' => 'jr@example.com' ,
                           
'AGE' => 32 ,
                           
'SALARY' => 65000
                         
];
$records[] = [
                           
'FIRST' => 'Bonnie' ,
                           
'LAST' => 'Socket' ,
                           
'EMAIL' => 'ba@example.com' ,
                           
'AGE' => 12 ,
                           
'SALARY' => 75000
                         
];
$records[] = [
                           
'FIRST' => 'Ronnie' ,
                           
'LAST' => 'Locket' ,
                           
'EMAIL' => 'rl@example.com' ,
                           
'AGE' => 32 ,
                           
'SALARY' => 85000
                         
];
$alwaysBreak = true;
$columns = [
                          
'FIRST' => '',
                          
'LAST' => '',
                          
'EMAIL' => '',
                          
'AGE' => '',
                          
'SALARY' => ''
                         
];
$groups = [ 'LAST' , 'AGE' ];
$map = [ 'LAST NAME' , 'AGE' ];
$totals = [ 'AGE' , 'SALARY' ];
$report = new ReportGrouping($records, $columns, $groups, $map, $totals, $alwaysBreak);
$report->headerCallback = 'outputHeader';
$report->lineCallback = 'outputLine';
$report->subTotalField = 'LAST';
?>
<!doctype html>
<style type="text/css">
  body
  {
    display : flex;
    font : 11px courier new;
    text-align : left;
    unicode-bidi : embed;
    white-space : pre;
  }
</style>
<body>
  <span style="flex:0 0 35%; font-size:9px;"> <?= print_r($report->records, true) ?>
</span>
  <span style="flex:0 0 65%;">
<?php
echo '<table border=0 cellspacing=0 cellpadding=3>'; $report->printReport(); echo '</table>'; ?>
</span>
<?php
//////////////////////////////////////////////////////////
// supporting functionality
function outputHeader()
{
 
$html = <<<EOS
<thead>
      <tr style="font:19px bold;">
        <td valign=bottom>Last Name</td>
        <td valign=bottom>First Name</td>
        <td valign=bottom>Email</td>
        <td align=right valign=bottom>Age</td>
        <td align=right valign=bottom>Salary</td>
      </tr>
    </thead>
EOS;
  echo
$html;
}
function
outputLine($record, $alternate = false, $bold = false) {
 
$bold = $bold ? 'bold' : 'normal';
 
$rowclass = $alternate ? 'altrowcolor' : 'rowcolor';
 
$html = "
    <tr class="
. $rowclass . " style='font-weight:" . $bold . ";'>
      <td>"
. $record['LAST'] . "</td>
      <td>"
. $record['FIRST'] . "</td>
      <td>"
. $record['EMAIL'] . "</td>
      <td align=right>"
. $record['AGE'] . "</td>
      <td align=right>"
. number_format($record['SALARY']) . "</td>
    </tr>"
;
  echo
$html;
}


  Files folder image Files  
File Role Description
Accessible without login Plain text file Example.php Example Implementation Of ReportGrouping.php class
Plain text file ReportGrouping.php Class Dynamic grouping and subtotaling class.

 Version Control Unique User Downloads Download Rankings  
 0%
Total:177
This week:0
All time:8,733
This week:94Up