ホームページ  >  記事  >  バックエンド開発  >  arraynetworks PHP 配列クロス集計の実装コード

arraynetworks PHP 配列クロス集計の実装コード

WBOY
WBOYオリジナル
2016-07-29 08:43:261083ブラウズ

SQL文を使うと負荷がかかりすぎるので、早速、コードを見てみましょう

コードをコピーします コードは次のとおりです。以下のように:


/**
* 基本的なクロス集計
* @author ヒュー
*
*/
class Pivot
{
private $HORIZONTAL_TOTAL_FIELD = 'total';
private $topPivot;
private $measure;
private $horizo​​ntalColumn = 配列();
private $isVerticalTotal = true;プライベート $horizo​​ntalTotal = null;
private $title = 'PivotTab';
プライベート関数 InitPivot()
$this->topPivot; $this->data as $d )
{
$this->horizo​​ntalColumn [] = $d [$this->leftPivot];
$this->verticalColumn [] = $d [$this-> ;topPivot];
$this->horizo​​ntalColumn = array_unique ( $this->verticalColumn );
foreach ( $this->horizo​​ntalColumn 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->horizo​​ntalColumn as $i )
{
$rowsum = 0;
foreach ( $this->verticalColumn as $j )
{
$rowsum += $this->pivotValue [$i] [$j]
}
$this->pivotValue [$i] [$this->TOTAL_FIELD] = $rowsum; }
/**
* 垂直合計を設定します
*/
プライベート関数 set水平Total()
{
$this->horizo​​ntalColumn [] = $this->HORIZONTAL_TOTAL_FIELD;
foreach ( $this->verticalColumn as $i )
{
$rowsum = 0;
foreach ( $this->horizo​​ntalColumn 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 = "n"; .= "n";
foreach ( $this->verticalColumn as $value )
{
$resault .= "n";
}
$ resault .= "n";
foreach ( $this->horizo​​ntalColumn as $i )
{
$resault .= "< ;td>$in";
foreach ( $this->pivotValue [$i] as $value )
{
$resault .= "n";
}
$resault .= "}
$resault .= "
$ this->title$ value
$value
";
return $resault
/**
* テーブルとしてレンダリングします
*/
function __construct (配列 $data, $topPivot, $leftPivot, $ メジャー)
{
$this->data = $data;
$this->topPivot = $topPivot; $this->measure = $measure;
$this->horizo​​ntalColumn = array ();
$this->InitPivot; fillData ();
}
}


重要なポイント これは InitPivot メソッドと fillData メソッドにあります。
InitPivot は、すべての項目が値を持つことを保証します (デフォルトは 0)
fillData メソッドは、選択および入力のメソッドを使用して、データをインストールする $pivotValue にデータを入力します。
あとはお好みで出力してください。
上記では、arraynetworks の PHP Array クロスタブの実装コードを紹介します。これには、arraynetworks の内容も含まれており、PHP チュートリアルに興味のある友人の参考になれば幸いです。


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。