Приглашаем посетить
Мода (modnaya.ru)

Hack 56. Create User-Customizable Reports

Previous
Table of Contents
Next

Hack 56. Create User-Customizable Reports

Hack 56. Create User-Customizable Reports Hack 56. Create User-Customizable Reports

Use a PHP reporting engine that takes an XML definition file and creates a custom report.

Reporting engines allow end users to customize the reports generated in their applications. This is extremely valuable in enterprise applications because these systems rarely are exactly what the customer wants. The ability to tweak the reports, notifications, or other front-facing features is critical for a satisfying user experience.

A reporting engine gives the user a declarative method for specifying a report. The host page sets up the query, gets the data, and then runs the report engine to format the data. Some reporting engines, like RLIB (http://rlib.sf.net/), can export not only to HTML, but also to PDF, XML, and other formats. In this hack, I use the PHPReports system (http://phpreports.sf.net/) to implement a simple book report.

6.7.1. The Code

Save the code in Example 6-11 as index.php.

Example 6-11. The PHP that runs the report generator
<?php
require_once( "PHPReportMaker.php" );
$rep = new PHPReportMaker();
$rep->setUser( "root" );
$rep->setPassword( "" );
$rep->setDatabaseInterface( "mysql" );
$rep->setConnection( "localhost" );
$rep->setDatabase( "books" );
$rep->setSQL( "SELECT NAME,AUTHOR FROM BOOK ORDER BY NAME" );

$rep->setXML( "bookreport.xml" );

$rep->run();
?>

Save the code in Example 6-12 as bookreport.xml.

Example 6-12. The report's XML specification
<REPORT MARGINWIDTH="5" MARGINHEIGHT="5">
<TITLE>Book Report</TITLE>
<CSS>report.css</CSS>
<PAGE BORDER="0" SIZE="10" CELLSPACING="0" CELLPADDING="5">
</PAGE>
<GROUPS>
<GROUP NAME="author" EXPRESSION="AUTHOR">
<HEADER>
<ROW>
<COL CELLCLASS="header"><XHTML><i>Name</i></XHTML></COL>
<COL CELLCLASS="header">Author</COL>
</ROW>
</HEADER>
<FIELDS>
<ROW>
<COL TYPE="FIELD">NAME</COL>
<COL TYPE="FIELD">AUTHOR</COL>
</ROW> 
</FIELDS> 
<FOOTER> 
</FOOTER> 
</GROUP>
</GROUPS>
</REPORT>

Save the code in Example 6-13 as report.css.

Example 6-13. The CSS for the report
.header { font-weight: bold; }

6.7.2. Running the Hack

Download and install the PHPReports system per the instructions included with the download. Upload the files to the server and navigate to the index.php page in your browser. The result should look like Figure 6-13.

Figure 6-13. The formatted book report
Hack 56. Create User-Customizable Reports


Changing the look of the report is as easy as tweaking the XML in the bookreport.xml file. The PHPReports system provides for control over the color, fonts, and layout of the report through both HTML and CSS. It also allows for data grouping through the inclusion of dynamic HTML elements like anchor tags and scripts.

PHPReports also has a flexible back end, allowing you to render to a single page of HTML, text, or even a multipage HTML report (the HTML is broken into multiple pages, and the generated markup has navigation at the bottom of each page). You can also create your own output plug-in, allowing you to create whatever output form you need.

6.7.3. See Also

  • "Give Your Customers Formatting Control with XSL" [Hack #7]


Previous
Table of Contents
Next