SQL文を使うと負荷がかかりすぎるので、早速、コードを見てみましょう
コードをコピーします コードは次のとおりです。以下のように:
/**
* 基本的なクロス集計
* @author ヒュー
*
*/
class Pivot
{
private $HORIZONTAL_TOTAL_FIELD = 'total';
private $topPivot;
private $measure;
private $horizontalColumn = 配列();
private $isVerticalTotal = true;プライベート $horizontalTotal = null;
private $title = 'PivotTab';
プライベート関数 InitPivot()
$this->topPivot; $this->data as $d )
{
$this->horizontalColumn [] = $d [$this->leftPivot];
$this->verticalColumn [] = $d [$this-> ;topPivot];
$this->horizontalColumn = array_unique ( $this->verticalColumn );
foreach ( $this->horizontalColumn as $h )
{
foreach ( $this->verticalColumn as $v )
{
$this->pivotValue [$h] [$v] = 0;
}
}
/**
* クロス集計を初期化します
* /
プライベート関数 fillData()
{
foreach ( $this->data as $row )
{
$this->pivotValue [$row [$this- >leftPivot]] [$row [$this- >topPivot]] += $row [$this->measure]
}
if ($this->is水平合計)
{
$this-> set水平合計 ();
}
if ($this->isVerticalTotal)
{
$this->setVerticalTotal ();
}
}
/**
* データを入力してください
*/
プライベート関数 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水平Total()
{
$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 '
'
print_r ( $this ->pivotValue );
/**
* レンダリング
*/
function RenderToTable()
{
$resault = "
$ this->title | n";$ value | n";
$value | n";