PHP Array cross table implementation code
- 2020-03-31 21:00:53
- OfStack
If the SQL statement is too much work, so try to write their own cross table class, all right, let's look at the code
The focus is on the InitPivot method and the fillData method.
InitPivot guarantees that all items will have a value (0 by default)
The fillData method USES the select fill add method to populate the $pivotValue of our data.
And then whatever output you like
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';
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;
}
}
}
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 ();
}
}
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;
}
}
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;
}
}
function Render()
{
echo '<pre>';
print_r ( $this->pivotValue );
}
function RenderToTable()
{
$resault = "<table border='1' width='250'>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->pivotValue [$i] as $value )
{
$resault .= "<td>$value</td>n";
}
$resault .= "</tr>n";
}
$resault .= "</table>";
return $resault;
}
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 the fillData method.
InitPivot guarantees that all items will have a value (0 by default)
The fillData method USES the select fill add method to populate the $pivotValue of our data.
And then whatever output you like