Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Generating Reports and Statistics in PHP : Page 4

Discover the PHP libraries that help you generate statistics and reports that analyze data from text files, XML, or relational databases.


advertisement

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:

   .HEADER {
      font-family: "arial","verdana";
      font-size: 12px;
      color: #ffffff;
      background: #000000;
      }
   .FOOTER {
      font-family: "arial","verdana";
      font-size: 12px;
      color: #ffffff;
      background: #000000;
      }
   .FIELDS {
      font-family: "arial","verdana";
      font-size: 10px;
      color: #0000cc;
      background: #ffffff;
      }

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):

   <FOOTER>
     <ROW>
       <COL CELLCLASS="FOOTER" ALIGN="RIGHT" 
         COLSPAN="3">Average of values:</COL>                      
       <COL ALIGN="LEFT" NUMBERFORMATEX="2" TYPE="EXPRESSION">
           $this->getAvg("currencyvalue")</COL>
     </ROW>
     <ROW>   
       <COL CELLCLASS="FOOTER" ALIGN="RIGHT" 
         COLSPAN="4">Copyright PHPReports 2008</COL>             
     </ROW>
   </FOOTER>   
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:

    1. 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.
    2. Use the PHPReports putEnvObj() function to send an instance of the class created in step 1 to the report layout.
    3. Use the SQL query: select * from currencies;
    4. Modify the report layout so it calls the ReportToGraph class methods. For this, the TYPE attribute will have the value RAW_EXPRESSION.
    5. Step 1: The ReportToGraph.class.php code is in Listing 5.
    6. Steps 2 and 3: The reportPHP.php file code looks like this:

<?php
      // include the PHPReports classes on the PHP path! 
      // Configure your path here 
      ini_set("include_path,
         "ini_get("include_path").";
         C:\Program Files\Apache Group\
            Apache2\htdocs\php\statisticreport\phpreports");
      require_once("PHPReportMaker.php");
      require_once("ReportToGraph.class.php");
           
      //create an instance of ReportToGraph
      $rtg = new ReportToGraph();
   
      //SQL query
      $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.


      $oRpt->setXML("reportXML.xml");
      $oRpt->setUser("root");
      $oRpt->setPassword("");
      $oRpt->setConnection("localhost");
      $oRpt->setDatabaseInterface("mysql");
      $oRpt->setSQL($sSQL);
      $oRpt->setDatabase("usdeurgbp");
      $oRpt->putEnvObj("myclass,"$rtg);
      $oRpt->run();
   ?>
  1. 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.



Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book "XML Technologies—XML in Java" (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she's interested in software architecture, web services, UML, and high-performance unit tests.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap