Tutorial on php Report Class.

Lesson 3 - Doing math

We now want to make a real report with the data we have. Actually, we will change the data a little, and add some more data lines and one more column.

Your new data

<?php

$list
[] = array( 
            
"location"    => "Store 1",
            
"description" => "Description 1",
            
"inserted"    => "2004-01-01",
            
"qtd"         => "3",
            
"value"       => "1.23"
        
);
$list[] = array( 
            
"location"    => "Store 1",
            
"description" => "Description 2",
            
"inserted"    => "2004-02-01",
            
"qtd"         => "2",
            
"value"       => "3.21"
        
);
$list[] = array( 
            
"location"    => "Store 1",
            
"description" => "Description 3",
            
"inserted"    => "2004-03-01",
            
"qtd"         => "1",
            
"value"       => "2.31"
        
);
$list[] = array( 
            
"location"    => "Store 2",
            
"description" => "Description 4",
            
"inserted"    => "2004-04-01",
            
"qtd"         => "3",
            
"value"       => "5.10"
        
);
$list[] = array( 
            
"location"    => "Store 2",
            
"description" => "Description 5",
            
"inserted"    => "2004-04-01",
            
"qtd"         => "3",
            
"value"       => "2.50"
        
);
?>
The new column will be used to split some of the results according with its store location in our 'finance like report'. And we want to know how much was sold in both places.
 
The new HTML template

<html>

<head>
<title>Lesson 2 </title>
</head>
<body>
<h2>Lesson 3 report:</h2>
<table width="90%" border="1" cellpadding="1" cellspacing="2" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
  <!-- BEGIN b_location -->
  <tr> 
    <td colspan="4" bgcolor="#CCCCCC"><strong><font size="3">{location}</font></strong></td>
  </tr>
  <tr> 
    <td bgcolor="#EEEEEE"><font size="2"><strong>Description</strong></font></td>
    <td bgcolor="#EEEEEE"><font size="2"><strong>Date inserted</strong></font></td>
    <td bgcolor="#EEEEEE"><font size="2"><strong>Qtd</strong></font></td>
    <td bgcolor="#EEEEEE"><font size="2"><strong>Value (R$)</strong></font></td>
  </tr>
  <!-- BEGIN values -->
  <tr> 
    <td>{description}</td>
    <td>{inserted}</td>
    <td>{qtd}</td>
    <td>{value} </td>
  </tr>
  <!-- END values -->
  <!-- BEGIN totals -->
  <tr> 
    <td colspan="2" align="right"><strong>Total</strong></td>
    <td>{qtd}</td>
    <td>{value}</td>
  </tr>
  <!-- END totals -->
  <!-- END b_location -->
</table>
<a href="javascript:history.back()">back</a>
</body>
</html>
Besides the block named 'values' we now added a new one, called b_location. That block will be repeated once for each different location values. Note that we dont have a b_location column, but one named location. That b_ prefix is added by the Report class to avoid conflict within the template block processing. You dont have to change that prefix, but must be aware of that change since is made by the Report class every time you add headers or sub-headers to your data. One last block was added, the totals is a standard block used by the Report class to put the results of columns sums. It must be inside the b_location block in this case, since there are sums for each different location. Note that the tags inside the totals block have the same name as the tags above it.

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


<?php

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

 
// Read the array 
 
require_once("array-2.php");
 
// Create the report
 
$report =& new Report($list".""lesson-3.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;
 // Define columns where we want total
 
$sums = array ( 'qtd' => true'value' => true);
 
$report->setTotals($sums);
 
// Define wich column should be used as header
 
$report->setTitle('location'0);
 
// Uncoment bellow if you need debug the template processing
 //$report->debug = true;
 // Prepare the data
 
$report->makeReport();
 
// Show it
 
$report->show();
 
?>

To see the result click here.


Nice hum? But our report is wrong! We forgot to multiply the quantity by price of goods sold for each line! The total reported for each store is completly wrong. To fix that we need to multiply qtd*value and create a new column to place the result, and then add the result.

To have a new column we need to modify the template and open space for it and change the php code to to the math for us:

The new HTML template

<html>

<head>
<title>Lesson 2 </title>
</head>
<body>
<h2>Lesson 3 report:</h2>
<table width="90%" border="1" cellpadding="1" cellspacing="2" bordercolor="#FFFFFF" bgcolor="#FFFFFF">
  <!-- BEGIN b_location -->
  <tr> 
    <td colspan="5" bgcolor="#CCCCCC"><strong><font size="3">{location}</font></strong></td>
  </tr>
  <tr> 
    <td bgcolor="#EEEEEE"><font size="2"><strong>Description</strong></font></td>
    <td bgcolor="#EEEEEE"><font size="2"><strong>Date inserted</strong></font></td>
    <td bgcolor="#EEEEEE"><font size="2"><strong>Qtd</strong></font></td>
    <td bgcolor="#EEEEEE"><font size="2"><strong>Values</strong></font></td>
    <td bgcolor="#EEEEEE"><font size="2"><strong>Sub-total (R$)</strong></font></td>
  </tr>
  <!-- BEGIN values -->
  <tr> 
    <td>{description}</td>
    <td>{inserted}</td>
    <td>{qtd}</td>
    <td>{value} </td>
    <td>{subtotal} </td>
  </tr>
  <!-- END values -->
  <!-- BEGIN totals -->
  <tr> 
    <td colspan="2" align="right"><strong>Total</strong></td>
    <td>{qtd}</td>
    <td>&nbsp;</td>
    <td>{subtotal}</td>
  </tr>
  <!-- END totals -->
  <!-- END b_location -->
</table>
<a href="javascript:history.back()">back</a>
</body>
</html>
Note that the only change made was the new column. We could have used the same number of columns if we wanted. We could have replaced the the tag {value} to {subtotal}. That should work too.
The new php code for the right report:

<?php

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

 
// Read the array 
 
require_once("array-2.php");
 
// Create the report
 
$report =& new Report($list".""lesson-3a.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;
 // Calculate the sale values
  
$report->setExpression("subtotal""{qtd}*{value}""money");
 
// Define columns where we want total
 
$sums = array ( 'qtd' => true'subtotal' => true);
 
$report->setTotals($sums);
 
// Define wich column should be used as header
 
$report->setTitle('location'0);
 
// Uncoment bellow if you need debug the template processing
 //$report->debug = true;
 // Prepare the data
 
$report->makeReport();
 
// Show it
 
$report->show();
 
?>
We have used the method setExpression to get the job done. The first argument is the name of the new field, the second is the math expression to be used. Note that you can use any function here, php functions or user defined ones. The last argument is the field type. If omitted the type will be the default type, that is a string.

To see the result click here.


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