If the SQL statement is used, the workload is too large, so I try to write a cross-Table class by myself. Let's take a look. Code
Copy code The Code is as follows :/**
* Basic cross tabulation
* @ Author Hugh
*
*/
Class Program
{
Private $ horizontal_total_field = 'Total ';
Private $ vertical_total_field = 'Total ';
Private $ data;
Private $ toppivot;
Private $ leftpipeline;
Private $ measure;
Private $ horizontalcolumn = array ();
Private $ verticalcolumn = array ();
Private $ effectvalue = array ();
Private $ ishorizontaltotal = true;
Private $ isverticaltotal = true;
Private $ horizontaltotal = NULL;
Private $ verticaltotal = NULL;
Private $ Title = 'effecttab ';
/**
* Initialize the crosstab chart.
*/
Private function initpivot ()
{
$ This-> toppivot;
Foreach ($ this-> data as $ D)
{
$ This-> horizontalcolumn [] = $ d [$ this-> leftpipeline];
$ This-> verticalcolumn [] = $ d [$ this-> toppivot];
}
$ This-> horizontalcolumn = array_unique ($ this-> horizontalcolumn );
$ This-> verticalcolumn = array_unique ($ this-> verticalcolumn );
$ Reasult = array ();
Foreach ($ this-> horizontalcolumn as $ H)
{
Foreach ($ this-> verticalcolumn as $ V)
{
$ This-> effectvalue [$ H] [$ v] = 0;
}
}
}
/**
* Fill in data
*/
Private function filldata ()
{
Foreach ($ this-> data as $ row)
{
$ This-> effectvalue [$ row [$ this-> lefttings] [$ row [$ this-> toppivot] + = $ row [$ this-> measure];
}
If ($ this-> ishorizontaltotal)
{
$ This-> sethorizontaltotal ();
}
If ($ this-> isverticaltotal)
{
$ This-> setverticaltotal ();
}
}
/**
* Set vertical sum
*/
Private function setverticaltotal ()
{
$ This-> verticalcolumn [] = $ this-> vertical_total_field;
Foreach ($ this-> horizontalcolumn as $ I)
{
$ Rowsum = 0;
Foreach ($ this-> verticalcolumn as $ J)
{
$ Rowsum + = $ this-> effectvalue [$ I] [$ J];
}
$ This-> effectvalue [$ I] [$ this-> total_field] = $ rowsum;
}
}
/**
* Set horizontal sum
*/
Private function sethorizontaltotal ()
{
$ This-> horizontalcolumn [] = $ this-> horizontal_total_field;
Foreach ($ this-> verticalcolumn as $ I)
{
$ Rowsum = 0;
Foreach ($ this-> horizontalcolumn as $ J)
{
$ Rowsum + = $ this-> effectvalue [$ J] [$ I];
}
$ This-> effectvalue [$ this-> horizontal_total_field] [$ I] = $ rowsum;
}
}
/**
* Rendering
*/
Function render ()
{
Echo '<PRE> ';
Print_r ($ this-> effectvalue );
}
/**
* Rendering as table
*/
Function rendertotable ()
{
$ Resault = "<Table border = '1' width = '000000'> \ n ";
$ Resault. = "<tr> <TD> $ this-> title </TD> \ n ";
Foreach ($ this-> verticalcolumn as $ value)
{
$ Resault. = "<TD> $ value </TD> \ n ";
}
$ Resault. = "</tr> \ n ";
Foreach ($ this-> horizontalcolumn as $ I)
{
$ Resault. = "<tr> <TD> $ I </TD> \ n ";
Foreach ($ this-> effectvalue [$ I] as $ value)
{
$ Resault. = "<TD> $ value </TD> \ n ";
}
$ Resault. = "</tr> \ n ";
}
$ Resault. = "</table> ";
Return $ resault;
}
/**
* Construct a crosstab chart
* @ Param $ data Data Source
* @ Param $ toppivot header field
* @ Param $ leftpipeline left column Field
* @ Param $ measure calculation amount
*/
Function _ construct (array $ data, $ toppivot, $ lefttransform, $ measure)
{
$ This-> DATA = $ data;
$ This-> leftpipeline = $ leftpipeline;
$ This-> toppivot = $ toppivot;
$ This-> measure = $ measure;
$ This-> horizontalcolumn = array ();
$ This-> verticalcolumn = array ();
$ This-> init.pdf ();
$ This-> filldata ();
}
}
The focus is on the initpivot method and filldata method.
Initpivot ensures that all items have a value (0 by default)
The filldata method fills the data into the $ ttvalue of the loaded data using the method of adding the selected padding.
Then I like how to output them all.