tatistics and reports analyze the change over time of any kind of phenomena. For example, you could evaluate an employer’s performance by analyzing progress curves provided by reports; managers can make business decisions based on statistical sales data; meteorologists can predict natural disasters based on statistical weather pattern data—and the list goes on. For the software industry, statistics and reports provide both an ongoing challenge and an ongoing market. At present, programming languages such as PHP and Java come with built-in packages for developing applications around statistical problems.
This article explores PHP’s support for the statistical domain. You will see how to generate reports and statistics for simple text phrases, XML documents, and complex databases.
The Text_Statistics PEAR Package
The Text_Statistics PEAR package makes it easy to calculate some basic readability metrics on blocks of text. These metrics include such things as the number of words, the number of unique words, the number of sentences, and the number of total syllables. You can use these statistics to calculate the Flesch score for a sentence, which is a number between 0 and 100 that represents readability. Figure 1 shows the formula for the Flesch Reading Ease Score (FRES) test.
![]() | |
Figure 1. Flesch Reading Ease Score Formula: This formula calculates the relative readability of a text; the higher the score, the easier the text is to read. |
The higher the score, the more readable the text (high scores have larger potential audiences). For example, a Flesch score between 90 and 100 equates to a fifth-grade reading level; a Flesch score between 0 and 30 means the text may be readable only by college graduates. This tutorial provides more details about the Flesch readability formula and the Flesch readability tests (Flesch—Kincaid Grade Level).
You install the PEAR package like this (version 1.0 is the stable version):
pear install Text_Statistics
As you will see in the next two examples, the Text_Statistics PEAR is very easy to use, because the applications are straightforward and the code is very intuitive. For example, to retrieve the number of syllables in a word you can use the Text_Word class like this:
numSyllables()." syllables."); ?>
The output of this example is:
The word 'paragraphs' has 3 syllables.
Here’s a more complete example based on the Text_Statistics class that analyzes a complete (but still short) sentence. In this case the output contains more information, including the number of syllables, the number of unique words, the Flesch score, the abbreviations, and more:
The entire array:
"); print_r($stats); print_r("
"); print_r("Text: ".$stats->text."
"); print_r("Syllables: ".$stats->numSyllables."
"); print_r("Words number: ".$stats->numWords."
"); print_r("Unique words: ".$stats->uniqWords."
"); print_r("Sentences number: ".$stats->numSentences."
"); print_r("Flesch: ".$stats->flesch."
"); ?>
The output of this example shows both a raw array of data that the Text_Statistics package returns and a list of specific values extracted from that array and formatted more readably:
The entire array: Text_Statistics Object ( [text] => This is an example. [numSyllables] => 5 [numWords] => 4 [uniqWords] => 4 [numSentences] => 1 [flesch] => 97.025 [_abbreviations] => Array ( [/Mr./] => Misterr [/Mrs./i] => Misses [/etc./i] => etcetera [/Dr./i] => Doctor ) [_uniques] => Array ( [this] => 1 [is] => 1 [an] => 1 [example] => 1 ) ) Text: This is an example. Syllables: 5 Words number: 4 Unique words: 4 Sentences number: 1 Flesch: 97.025
You aren’t limited to analyzing text files, though; another PEAR package makes it easy to analyze data stored in XML.
The XML_Statistics PEAR Package
To analyze XML documents, you can install and use the XML_Statistics package, which provides methods for obtaining statistics about tags, attributes, entities, processing instructions, data blocks, and CDATA sections for any well-formatted XML document.
To install the PEAR, use this command (version 0.1 is the beta version):
> pear install -alldeps XML_Statistics-0.1
Authors’ Note Use the -alldeps option when installing, because the XML_Statistics PEAR depends on the XML_Parser PEAR package, which is an XML parser based on PHP’s built-in XML extension. The latest released of the XML_Parser PEAR is 1.2.8 (stable). |
After installing the package, you’ll find the code for the base class of the XML_Statistics package in the file XML_Statistics.php. Here are some of its main functions:
- boolean analyzeFile(mixed $file,string $filename): This function analyzes an XML file by loading it from a file path or URL. To see the results of the analysis use the countX() and getX() methods.
- integer countTag([string $tagname = null]): This function returns the number of occurrences of a tag in the entire XML document. The tag name is passed to the function through the $tagname argument.
- integer countAttribute([string $attribute = null], [string $tagname = null]): This function returns the number of occurrences of an attribute. You pass the attribute name to the function via the $attribute argument. If you don’t specify the second argument, $tagname, then the function searches for the specified attribute in the entire XML document; otherwise, it limits the search range to the specified tag.
- integer getMaxDepth(): This function returns the maximum nesting level in the document, the “depth” of the XML tree.
- integer countTagsInDepth(integer $depth): This function returns the number of tags that “live” at the specified depth. The root tag depth is zero.
- integer countExternalEntity([string $name = null]): This function returns the number of occurrences of external entities. If you don’t specify an entity name then the function counts the total number of external entities; otherwise, it counts only the occurrences of the specified entity.
- integer getCDataLength(): This function return the total length of all CDATA sections.
Currency Report #FFFFFF SELECT * FROM currencies WHERE dayref BETWEEN '2008-02-01' AND '2008-11-01' AND currency='USD' localhost mysql usdeurgbp No data was found, check your query // no modification from here
And the PHP code looks like this:
'2008-02-01' AND dayref < '2008-11-01' AND currency='USD'"; //Create the report maker object $oRpt = new PHPReportMaker(); //configure the XML file $oRpt->setXML("reportXML.xml"); //configure the database connection $oRpt->setUser("root"); $oRpt->setPassword(""); $oRpt->run(); ?>
You’ve seen a quick-and-dirty overview of generating reports from databases. Fortunately, you’re not limited to simple text or table output; you can use CSS to customize the appearance of your reports, or go beyond that to generate graphical charts using SVG.
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
You aren’t limited to transferring data directly from the SQL results to the screen. For example, the
- getValue(
) : Returns the current value of the field on the current group. - getSum(
) : Returns the sum of the field on the current group. - getMax(
) : Returns the maximum value of the field on the current group. - getMin(
) : Returns the minimum value of the field on the current group. - getAvg(
) : 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(
) : 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 of the report layout like this (the rest of code remain the same):
Author’s Note: To use a function in a |
After making the modification, the rendered report looks like Figure 4:
![]() | |
Figure 4. Report with Calculated Average: Using a |
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:
![]() | |
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(); ?>
- 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.