一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

PHP Array交叉表實(shí)現(xiàn)代碼

如果使用sql語句做的話 工作量太大了,于是嘗試自己寫一個(gè)交叉表的類,好二話不說,我們看看代碼
復(fù)制代碼 代碼如下:
/**
* 基本交叉表
* @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';
/**
* 初始化交叉表
*/
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;
}
}
}
/**
* 填充數(shù)據(jù)
*/
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 ();
}
}
/**
* 設(shè)置縱向合計(jì)
*/
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;
}
}
/**
* 設(shè)置橫向合計(jì)
*/
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 );
}
/**
* 渲染為table
*/
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;
}
/**
* 構(gòu)造交叉表
* @param $data 數(shù)據(jù)源
* @param $topPivot 頭欄目字段
* @param $leftPivot 左欄目字段
* @param $measure 計(jì)算量
*/
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 ();
}
}

重點(diǎn)在于InitPivot方法及fillData方法。
InitPivot里面保證了所有的item都會有值(默認(rèn)為0)
fillData方法使用選擇填充添加的方法,將數(shù)據(jù)填充入我們裝數(shù)據(jù)的$pivotValue里面。

然后喜歡怎么輸出都可以了

php技術(shù)PHP Array交叉表實(shí)現(xiàn)代碼,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产一级特黄aa级特黄裸毛片 | 五月婷婷俺来也 | 五月婷婷丁香综合 | 黄色网络在线观看 | 中文字幕精品视频在线 | 黄站视频| 99在线精品日韩一区免费国产 | 午夜视频国语 | 99午夜 | 久久国产精品1区2区3区网页 | 视频一区二区在线观看 | 激情小说 激情图片 | 色婷婷在线播放 | 国产成人精品影视 | 久久免费精品国产72精品剧情 | 亚洲国产激情在线一区 | 人成精品视频三区二区一区 | 欧美在线精品永久免费播放 | 黄色日韩网站 | 国产超91 | 色综合色狠狠天天综合色 | 中文字幕在线视频第一页 | 久久精品国产99久久72 | 麻豆网页| 白丝美女被狂躁视频免费网站 | 亚洲最大的视频网站 | 亚洲国产夜色在线观看 | 久久国产影视免费精品 | 久久精品亚洲欧美日韩久久 | 欧美日韩看看2015永久免费 | 四虎入口 | 亚洲国产成人久久综合野外 | 都市激情亚洲色图 | 91久久国产情侣真实对白 | 日本人妖系列 | 中文字幕日韩一区二区三区不 | 久久久久久网站 | 女人与拘一级毛片 | 免费看91视频 | 国内色综合精品视频在线 | 一区二区三区四区亚洲 |