Home  >  Article  >  Backend Development  >  PHP Array crosstab implementation code_PHP tutorial

PHP Array crosstab implementation code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:34:45955browse

If you use SQL statements to do it, the workload will be too much, so I try to write a crosstab class myself. Without further ado, let’s take a look at the code

Copy code The code is as follows:

/**
* Basic Crosstab
* @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 crosstab
*/
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 in 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 total
*/
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 total
*/
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 .= "n";
foreach ( $this->verticalColumn as $value )
{
$resault .= "n ";
}
$resault .= "n";
foreach ( $this->horizontalColumn as $i )
{
$resault .= "< ;tr>n";
foreach ( $this->pivotValue [$i] as $value )
{
$resault .= "n";
}
$resault .= "
n";
}
$resault .= "
$ this->title$value
$i
";
return $resault;
}
/**
* Construct crosstab
* @param $data data source
* @param $topPivot header column 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 focus is on the InitPivot method and fillData method.
InitPivot ensures that all items will have values ​​(default is 0)
The fillData method uses the method of selecting and filling to fill the data into the $pivotValue where we install the data.

Then you can output it however you like.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322362.htmlTechArticleIf you use sql statements, the workload will be too much, so I tried to write a crosstab class myself. It’s easy. Not to mention, let’s take a look at the code copy code. The code is as follows: /** * Basic crosstab...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn