Title: ReportList Class Marked Cool (Review this resource) Author: dclark13 Posted On: 2004-12-01 Category: Home > PHP Classes
Popularity:
Description: Quickly output data arrays, especially query results. Features record linking (i.e. drill-down support), pagination, column sorting, row highlighting, and downloading to delimited text files and XML.
Total Hits: 2076 Total Votes: 6
Total Points: 23 (6 reviews) [ Download ]
ReportList - Example Usage
April 2004
Purpose
The ReportList class was originally designed as the foundation to an online reporting system for a database-driven
website. It was hoped that it could be used to greatly simplify the migration of a set of database query results to an
HTML page, with full user navigation control and export options that would allow power users to analyze data offline, in
programs like Excel or OpenOffice Calc. Over the course of several years now the class has lived up to these expectations
in numerous projects and has grown to support many more features than I originally imagined. Below is a brief example
of some of the more common methods and usages of the ReportList class.
Preface
For the following examples, assume that the $arrData array is set up as the following (more likely
your array will have been populated by a database query, but the format will be similar):
$arrData = array();
array_push($arrData, array('strFName'=>'David', 'strLName'=>'Clark',
'strHomePhone'=>'8012541691', 'strState'=>'ut', 'curAmountPaid'=>123.45));
array_push($arrData, array('strFName'=>'Valerie', 'strLName'=>'Smith',
'strHomePhone'=>'8019746721', 'strState'=>'in', 'curAmountPaid'=>4914.23));
array_push($arrData, array('strFName'=>'Thomas', 'strLName'=>'Edison',
'strHomePhone'=>'5191220', 'strState'=>'ut', 'curAmountPaid'=>45.78));
array_push($arrData, array('strFName'=>'Marie', 'strLName'=>'Curie',
'strHomePhone'=>'8238485', 'strState'=>'ct', 'curAmountPaid'=>798));
array_push($arrData, array('strFName'=>'Isaac', 'strLName'=>'Asimov',
'strHomePhone'=>'8609784612', 'strState'=>'ct', 'curAmountPaid'=>1540.1));
Example 1 - Instantiation & Output
You have an array of data ($arrData) that you want to output as quickly and easily as possible in a standard
HTML table.
require_once('ReportList.php');
$oList = new ReportList();
$oList->makeListFromArray($arrData);
This will output the following table:
Record(s) 1 to 5 of 5 total.
| strFName | strLName | strHomePhone | strState | curAmountPaid |
| David | Clark | 8012541691 | ut | 123.45 |
| Valerie | Smith | 8019746721 | in | 4914.23 |
| Thomas | Edison | 5191220 | ut | 45.78 |
| Marie | Curie | 8238485 | ct | 798 |
| Isaac | Asimov | 8609784612 | ct | 1540.1 |
Example 2 - Specifying Headings
You need to output the HTML table for the $arrData array, but you want to add some title/sub-title
information. You'd also like to rename the columns so that they are more appropriate for display - to do this,
you must manually specify each column that you want to show. [As before, you must still call the
makeListFromArray function to actually output any HTML.]
$oList->setTitle('Customer Search Results');
$oList->setSubTitle('"Amount paid" figures are as of quarter-end.');
$oList->addOutputColumn('strFName', 'First Name');
$oList->addOutputColumn('strLName', 'Last Name');
$oList->addOutputColumn('strHomePhone', 'Home Phone');
$oList->addOutputColumn('strState', 'State');
$oList->addOutputColumn('curAmountPaid', 'Amount Paid');
This will output the following table:
Customer Search Results "Amount paid" figures are as of quarter-end. Record(s) 1 to 5 of 5 total.
| First Name | Last Name | Home Phone | State | Amount Paid |
| David | Clark | 8012541691 | ut | 123.45 |
| Valerie | Smith | 8019746721 | in | 4914.23 |
| Thomas | Edison | 5191220 | ut | 45.78 |
| Marie | Curie | 8238485 | ct | 798 |
| Isaac | Asimov | 8609784612 | ct | 1540.1 |
Example 3 - Formatting Data
In our examples so far the "Home Phone" and "Amount Paid" data isn't formatted at all. It would be nice to show
these properly as phone numbers, and (U.S.) currency figures. Also, states are normally all upper-case. To do so,
modify the calls to the addOutputColumn method to specify data formats for each of these fields. While
we're doing that we might as well right-justify the "State" and "Amount Paid" column, since that is more typical of those
data formats than left-alignment.
$oList->addOutputColumn('strHomePhone', 'Home Phone', 'phone');
$oList->addOutputColumn('strState', 'State', 'ucase', 'right');
$oList->addOutputColumn('curAmountPaid', 'Amount Paid', 'dollars',
'right');
This will output the following table:
Customer Search Results "Amount paid" figures are as of quarter-end. Record(s) 1 to 5 of 5 total.
| First Name | Last Name | Home Phone | State | Amount Paid |
| David | Clark | (801) 254-1691 | UT | $123.45 |
| Valerie | Smith | (801) 974-6721 | IN | $4,914.23 |
| Thomas | Edison | 519-1220 | UT | $45.78 |
| Marie | Curie | 823-8485 | CT | $798.00 |
| Isaac | Asimov | (860) 978-4612 | CT | $1,540.10 |
|