Recently, cross tabulation is required, and the company's requirements are complicated. Common cross tabulation tools are not suitable for use. if SQL statements are used, the workload is too large, so I tried to write a cross-table class by myself. let's look at the 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'
';
Print_r ($ this-> effectvalue );
}
/**
* Rendering as table
*/
Function RenderToTable ()
{
$ Resault ="
\ N ";$ Resault. ="
$ This-> title | \ N ";Foreach ($ this-> verticalColumn as $ value){$ Resault. ="
$ Value | \ N ";}$ Resault. ="
\ N ";Foreach ($ this-> horizontalColumn as $ I){$ Resault. ="
$ I | \ N ";Foreach ($ this-> effectvalue [$ I] as $ value){$ Resault. ="
$ Value | \ N ";}$ Resault. ="
\ N ";}$ Resault. ="
";
Return $ resault;
}
/**
* Construct a crosstab chart
* @ Param $ 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.