How it works
First you need its a computer running Apache and
PHP, compiled with the
Sablotron libs or libxstl
to deal with XML/XSLT transformation.
I use all that stuff on Linux, but if you want to use on other OSs its your choice. :-)
What are these stuff (PHP classes, XML files, XSL transformation) about?
PHPReports files are used to transform XML report layout files into PHP code, used by pre-defined PHP classes to generate your report on the browser window.The PHPReports package comes with two of these things:
- XSL transformation file - its a file used to transform your XML report layout file into some PHP code used by the pre-defined PHP classes that comes with the package to generate the report.
- The PHP classes - are classes used to process the result of the XSL transformation above.
You'll need some understanding of how XML works, but its very simple, there's some good tutorial on the net.
The XML file
The main thing about the XML file is all based on three layers (check the colors on the example) :1 - The report layer, wich is the most external layer of the report. It handles all the values processed on the full report. | |
2 - The page layer handles all the values on the current page, reseting it on the end of page. | |
3 - The groups layers handles all the values on the current group, reseting it on the end of group. |
But what is a group ? A group is a report "break" based on some expression you have. For example, this kind of report:
John Doe Enterprises Sales Report |
||||
city: | Sao Jose do Rio Preto, SP | |||
id | name | product | $ | |
00001 | Eustaquio Rangel | Book - Linux Programming Guide | 25.00 | |
00002 | Ana Carolina | Book - Photoshop 7.0 | 22.50 | |
total | 47.50 | |||
city: Sao Paulo, SP | ||||
id | name | product | $ | |
00003 | Andre Kada | CD - Kreator - Violent Revolutions | 15.00 | |
total | 15.00 | |||
page total | 62.50 | |||
report total | 62.50 |
There's a total for each city, a total for the current page and a total for the whole report.
PHPReports can handle it all, with functions like SUM(), AVG(), MIN() and MAX().
The XML file for the example above will be something like this:
<REPORT>
<TITLE>Sales Report</TITLE>
<BACKGROUND_COLOR>#FFFFFF</BACKGROUND_COLOR>
<CSS>johndoe.css</CSS>
<DOCUMENT>
<FOOTER>
<ROW>
<COL COLSPAN="3" TEXTCLASS="BOLD" ALIGN="RIGHT">report total</COL>
<COL TYPE="EXPRESSION" TEXTCLASS="BOLD" ALIGN="RIGHT" NUMBERFORMATEX="2">$this->getSum( "VALUE" )</COL>
</ROW>
</FOOTER>
</DOCUMENT>
<PAGE>
<HEADER>
<ROW>
<COL COLSPAN="4" TEXTCLASS="BOLD">John Doe Enterprises</COL>
</ROW>
<ROW>
<COL COLSPAN="4" TEXTCLASS="BOLD">Sales Report</COL>
</ROW>
</HEADER>
<FOOTER>
<ROW>
<COL COLSPAN="3" TEXTCLASS="BOLD" ALIGN="RIGHT">page total</COL>
<COL TYPE="EXPRESSION" TEXTCLASS="BOLD" ALIGN="RIGHT">$this->getSum( "VALUE" )</COL>
</ROW>
</FOOTER>
</PAGE>
<GROUPS>
<GROUP NAME="CityBreak" EXPRESSION="CITY">
<HEADER>
<ROW>
<COL ALIGN="RIGHT">city:</COL>
<COL TYPE="EXPRESSION" COLSPAN="3" TEXTCLASS="BOLD">$this->getValue( "CITY" )</COL>
</ROW>
<ROW>
<COL>id</COL>
<COL>name</COL>
<COL>product</COL>
<COL>$</COL>
</ROW>
</HEADER>
<FOOTER>
<ROW>
<COL ALIGN="RIGHT">total</COL>
<COL TYPE="EXPRESSION" COLSPAN="3" TEXTCLASS="BOLD" ALIGN="RIGHT">$this->getSum( "VALUE" )</COL>
</ROW>
</FOOTER>
<FIELDS>
<ROW>
<COL TYPE="FIELD">ID</COL>
<COL TYPE="FIELD">NAME</COL>
<COL TYPE="FIELD">PRODUCT</COL>
<COL TYPE="FIELD" ALIGN="RIGHT" NUMBERFORMATEX="2">VALUE</COL>
</ROW>
</FIELDS>
</GROUP>
</GROUPS>
</REPORT>
It is not so difficult if you think your report as a HTML table (to be honest, IT IS a HTML table):
John Doe Enterprises |
|||
Sales Report | |||
city: | Sao Jose do Rio Preto, SP | ||
id | name | product | $ |
00001 | Eustaquio Rangel | Book - Linux Programming Guide | 25.00 |
00001 | Eustaquio Rangel | Book - Design Patterns | 35.00 |
00002 | Ana Carolina | Book - Photoshop 7.0 | 22.50 |
total | 82.50 | ||
city: Sao Paulo, SP | |||
id | name | product | $ |
00003 | Andre Kada | CD - Kreator - Violent Revolutions | 15.00 |
total | 15.00 | ||
page total | 97.50 | ||
report total | 97.50 |
See how it works ? Check the ROW and COL similarity (it's the same thing!) with HTML TR and TD.
The easiest way to make your report its get a piece of paper, draw it like a HTML table (no, don't go right to your computer to make it, its better on the piece of paper) and create the XML file. Don't forget that on the example above I'm not dealing with SQL queries and stuff like that.
The XSL file
What the XSL file does is take your XML report layout file and convert it to PHP code that uses the pre-defined classes of the package. You just need to use a function (makeReport, defined on the PHPReportMaker.php) or create a PHPReportMaker object and call its run method. XSL transformations is a cool thing to learn, if you want to know more theres also some good tutorials on internet, for example (for both XML and XSL) theres the guys from the W3Schools who makes a very good job teaching it for free.Check the samples section.