;

Simplify Reporting Output

Published March 17, 2009 | Development, Mobile, App | 0 Comments

Recently, I have been tasked having to generate several reports containing various metrics, and then export these into an Excel sheet so that the data can be processed further. I also occasionally get requests for data in a CSV format that can be imported into another system. And these are just for the one-off reports… most of the standard reporting tools I have written produce data in an XML format, which is then processed by an AJAX frontend and displayed in an HTML table to the user. Talk about messy.

So this morning, I finally sat down and wrote out a report abstraction class that has been floating around in my head for the past couple of months. Rather than have to worry about writing display code when building a report, I can simply deal with the data and hand it over to the class for display processing:

Building the Report Class
$Report = new Report( "My Report" );

if (isset($_GET['format'])) {
	$Report->Type = $_GET['format'];
}

$Report->Columns = array("Employee", "Department", "Salary");

foreach ($Employees as $Employee) {
	$Report->Rows[] = array($Employee->Name, $Employee->Department, $Employee->Salary);
}

$Report->Display();

From there, the class takes the type identifier (which in this case is being selected by the user and passed in the URL), and provides the data in the correct format. The class itself is fairly small, weighing in with less than 200 lines of code, but can display data in Excel, CSV, XML, HTML, or PDF, as well as a printer-optimized layout if the data is intended to be printed instead of displayed on-screen.

Displaying a Report
$Report = new Report( "My Report" );

if (isset($_GET['format'])) {
	$Report->Type = $_GET['format'];
}

$Report->Columns = array("Employee", "Department", "Salary");

foreach ($Employees as $Employee) {
	$Report->Rows[] = array($Employee->Name, $Employee->Department, $Employee->Salary);
}

$Report->Display();
;