Tutorial on php Report Class.

Lesson 2 - Formatting fields

Let's do some tricks now. We now want to format the date field to a 'user friendly one', like dd/mm/yyyy (european style) and show to the user that the last float field is money value. We also changed the template file to a nice formated one:

The HTML template

<html>

<head>
<title>Lesson 2 </title>
</head>
<body>
<h2>Lesson 2 report:</h2>
<table width="90%" border="1" cellpadding="1" cellspacing="2" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
  <tr bgcolor="#CCCCCC"> 
    <td><strong>Description</strong></td>
    <td><strong>Date inserted</strong></td>
    <td><strong>Qtd</strong></td>
    <td><strong>Value (R$)</strong></td>
  </tr>
  <!-- BEGIN values -->
  <tr> 
    <td>{description}</td>
    <td>{inserted}</td>
    <td>{qtd}</td>
    <td>{value} </td>
  </tr>
  <!-- END values -->
</table>
<a href="javascript:history.back()">back</a>
</body>
</html>

And changed the report to the code bellow, adding the definition for the fields 'inserted' and 'value' of our data:


<?php

/**
 * php Report lesson 2
 *
 * (c) 2004 Sebastiao Rocha A. Neto
 */
 
require_once("../src/Report.php");

 
// Read the array 
 
require_once("array-1.php");
 
// Create the report
 
$report =& new Report($list".""lesson-2.htm");
 
// Set column types
 
$types = array ( 'inserted' => 'date''value' => 'money');
 
$report->setVariableType($types);
 
// Uncomment bellow to change the Currency simbol.
 // $report->setCurrencySymbol('$');
 // Change bellow if you dont want to use european style for dates
 //$report->europeanstyle = false;
 // Prepare the data
 
$report->makeReport();
 
// Show it
 
$report->show();
 
?>

To see the result click here.

Notes:

The money format, by default, is coming as 'R$'. To change that uncoment the setCurrencySimbol call as shown in the code above.

The date format is in european style, to change that uncomment the 'europeanstyle' property as shown in the code above.

You can change the regex used to extract date and datetime fields too. To do that you call the method setDateRegex with the regex you want to use. See the code for details.

Negative values are formated using a string defined in the property _negativestyle If you need to use
another style for negative number, change this property using a html format of your choice.

Numeric values formating can be ajusted using methods defined in the class: getThousandSeparator(),
getDecimals() and getDecimalPoint(). Those methods are used to pass values to the number_format function from php, so any valid format there can be used here.

There 4 more field types you can use: datetime, numeric, percent. There is another one, called select used to handle form fields, but we wont discuss it here.

Locale support can be used as you saw. But here is a place where much more improvements can be made. What the class is missing is a easy way to setup locale definitions.

In the next lesson, we add a non-trivial header, and do some math with columns.
Click here to go to lesson 3


Google
Copyright © 2002-2004 Sebastião Rocha A. Neto