Adding CSS Support to Improve Report Design
The basic report is functional, but definitely not aesthetically appealing. You can improve your report's formatting by adding CSS styles. First, you need a CSS stylesheet that defines the styles you need, for example, the styles.css file below:
With the stylesheet in place, you can use its styles in a report layout like the one in Listing 4. The output now looks like Figure 3.
|Figure 3. CSS-Styled Report: This better-looking report uses a report layout that includes CSS styling.
Notice that the PHP code didn't change between the reports shown in Figure 2 and Figure 3.
Using Functions In <COL> Elements
You aren't limited to transferring data directly from the SQL results to the screen. For example, the <COL> element supports the following functions:
- getValue(<field name>): Returns the current value of the field on the current group.
- getSum(<field name>): Returns the sum of the field on the current group.
- getMax(<field name>): Returns the maximum value of the field on the current group.
- getMin(<field name>): Returns the minimum value of the field on the current group.
- getAvg(<field name>): Returns the average of the field on the current group (the sum divided by the row count).
- getRowCount(): Returns the current row count of the group.
- getRowNum(): Returns the current row number.
- getPageNum(): Returns the current page number.
- getParameter(<name>): Returns the value of the parameter with the specified name.
For example, if you want to obtain the average of the values for the last column of the above report, you can modify the <FOOTER> of the report layout like this (the rest of code remain the same):
<COL CELLCLASS="FOOTER" ALIGN="RIGHT"
COLSPAN="3">Average of values:</COL>
<COL ALIGN="LEFT" NUMBERFORMATEX="2" TYPE="EXPRESSION">
<COL CELLCLASS="FOOTER" ALIGN="RIGHT"
COLSPAN="4">Copyright PHPReports 2008</COL>
|Author's Note: To use a function in a <COL> element, specify the TYPE attribute with the EXPRESSION value.
After making the modification, the rendered report looks like Figure 4:
|Figure 4. Report with Calculated Average: Using a <COL> element with a type of "Expression," you can add features such as averages and totals to your reports.
Generating Graphical Reports
In this section you will see how to plot report values as graphs. To do that, you need the Image_Canvas PEAR—which is not a part of the PHPReports tool. You can find more details in this DevX article.
As an example, suppose you want to plot the values of USD and EUR currency for the year 2008. To achieve this, follow these steps:
- Develop a class (ReportToGraph.class.php) that will receive the SQL query results from PHPReports and will generate an SVG document (graphReport.svg) that represents the graph associated with the report.
- Use the PHPReports putEnvObj() function to send an instance of the class created in step 1 to the report layout.
- Use the SQL query: select * from currencies;
- Modify the report layout so it calls the ReportToGraph class methods. For this, the TYPE attribute will have the value RAW_EXPRESSION.
- Step 1: The ReportToGraph.class.php code is in Listing 5.
- Steps 2 and 3: The reportPHP.php file code looks like this:
// include the PHPReports classes on the PHP path!
// Configure your path here
C:\Program Files\Apache Group\
//create an instance of ReportToGraph
$rtg = new ReportToGraph();
$sSQL = "SELECT * FROM currencies";
//Create the report maker object
$oRpt = new PHPReportMaker();
|Figure 5. Graphical Report: The figure shows a line graph generated using the Image_Canvas package and a CSS-styled table.
- Step 4: The new report layout looks like Listing 6.
Finally, running the example will output the graph and table shown in Figure 5.
Obviously, this article provided only a bare-bones introduction to reports and statistics in PHP, but it should be enough to get you started so you can apply the techniques for your own needs.