PHPArray cross tabulation implementation code

Source: Internet
Author: User
Tags crosstab
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. =" \ N ";Foreach ($ this-> verticalColumn as $ value){$ Resault. =" \ N ";}$ Resault. =" \ N ";Foreach ($ this-> horizontalColumn as $ I){$ Resault. =" \ N ";Foreach ($ this-> effectvalue [$ I] as $ value){$ Resault. =" \ N ";}$ Resault. =" \ N ";}$ Resault. ="
$ This-> title$ Value
$ I$ Value
";
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.