If the workload is too large to use the SQL statement, then try to write a cross-table class, so apart, let's look at the code
Copy CodeThe code is as follows:
/**
* Basic cross-table
* @author Hugh
*
*/
Class Pivot
{
Private $HORIZONTAL _total_field = ' total ';
Private $VERTICAL _total_field = ' total ';
Private $data;
Private $topPivot;
Private $leftPivot;
Private $measure;
Private $horizontalColumn = Array ();
Private $verticalColumn = Array ();
Private $pivotValue = Array ();
Private $isHorizontalTotal = true;
Private $isVerticalTotal = true;
Private $horizontalTotal = null;
Private $verticalTotal = null;
Private $title = ' pivottab ';
/**
* Initialize cross-table
*/
Private Function Initpivot ()
{
$this->toppivot;
foreach ($this->data as $d)
{
$this->horizontalcolumn [] = $d [$this->leftpivot];
$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->pivotvalue [$h] [$v] = 0;
}
}
}
/**
* Fill Data
*/
Private Function Filldata ()
{
foreach ($this->data as $row)
{
$this->pivotvalue [$row [$this->leftpivot]] [$row [$this->toppivot]] + = $row [$this->measure];
}
if ($this->ishorizontaltotal)
{
$this->sethorizontaltotal ();
}
if ($this->isverticaltotal)
{
$this->setverticaltotal ();
}
}
/**
* Set Vertical totals
*/
Private Function Setverticaltotal ()
{
$this->verticalcolumn [] = $this->vertical_total_field;
foreach ($this->horizontalcolumn as $i)
{
$rowsum = 0;
foreach ($this->verticalcolumn as $j)
{
$rowsum + = $this->pivotvalue [$i] [$j];
}
$this->pivotvalue [$i] [$this->total_field] = $rowsum;
}
}
/**
* Set Horizontal totals
*/
Private Function Sethorizontaltotal ()
{
$this->horizontalcolumn [] = $this->horizontal_total_field;
foreach ($this->verticalcolumn as $i)
{
$rowsum = 0;
foreach ($this->horizontalcolumn as $j)
{
$rowsum + = $this->pivotvalue [$j] [$i];
}
$this->pivotvalue [$this->horizontal_total_field] [$i] = $rowsum;
}
}
/**
* Rendering
*/
function Render ()
{
Echo '
';
Print_r ($this->pivotvalue);
}
/**
* Render 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->pivotvalue [$i] as $value) {$resault. = "
$value | \ n "; } $resault. = "
\ n "; } $resault. = "
";
return $resault;
}
/**
* Construct cross-table
* @param $data Data source
* @param $topPivot header field
* @param $leftPivot left column field
* @param $measure Calculation amount
*/
function __construct (array $data, $topPivot, $leftPivot, $measure)
{
$this->data = $data;
$this->leftpivot = $leftPivot;
$this->toppivot = $topPivot;
$this->measure = $measure;
$this->horizontalcolumn = Array ();
$this->verticalcolumn = Array ();
$this->initpivot ();
$this->filldata ();
}
}
The emphasis is on Initpivot method and Filldata method.
Initpivot guarantees that all item will have a value (default is 0)
The Filldata method fills the data into the $pivotvalue of the data we are loading using the method of selecting fill addition.
And then like how to output everything.
http://www.bkjia.com/PHPjc/322362.html www.bkjia.com true http://www.bkjia.com/PHPjc/322362.html techarticle if the workload is too large to use the SQL statement, so try to write a cross-table class, good apart, we look at the code to copy the code as follows:/** * Basic cross-table ...