如果使用sql语句做的话 工作量太大了,于是尝试自己写一个交叉表的类,好二话不说,我们看看代码
<BR>/** <BR>* 基本交叉表 <BR>* @author hugh <BR>* <BR>*/ <BR>class Pivot <BR>{ <BR>private $HORIZONTAL_TOTAL_FIELD = 'total'; <BR>private $VERTICAL_TOTAL_FIELD = 'total'; <BR>private $data; <BR>private $topPivot; <BR>private $leftPivot; <BR>private $measure; <BR>private $horizontalColumn = array (); <BR>private $verticalColumn = array (); <BR>private $pivotValue = array (); <BR>private $isHorizontalTotal = true; <BR>private $isVerticalTotal = true; <BR>private $horizontalTotal = null; <BR>private $verticalTotal = null; <BR>private $title = 'PivotTab'; <BR>/** <BR>* 初始化交叉表 <BR>*/ <BR>private function InitPivot() <BR>{ <BR>$this->topPivot; <BR>foreach ( $this->data as $d ) <BR>{ <BR>$this->horizontalColumn [] = $d [$this->leftPivot]; <BR>$this->verticalColumn [] = $d [$this->topPivot]; <BR>} <BR>$this->horizontalColumn = array_unique ( $this->horizontalColumn ); <BR>$this->verticalColumn = array_unique ( $this->verticalColumn ); <BR>$reasult = array (); <BR>foreach ( $this->horizontalColumn as $h ) <BR>{ <BR>foreach ( $this->verticalColumn as $v ) <BR>{ <BR>$this->pivotValue [$h] [$v] = 0; <BR>} <BR>} <BR>} <BR>/** <BR>* 填充数据 <BR>*/ <BR>private function fillData() <BR>{ <BR>foreach ( $this->data as $row ) <BR>{ <BR>$this->pivotValue [$row [$this->leftPivot]] [$row [$this->topPivot]] += $row [$this<b>/本文来源gao@!dai!ma.com搞$$代^@码5网@</b><strong>搞代gaodaima码</strong>->measure]; <BR>} <BR>if ($this->isHorizontalTotal) <BR>{ <BR>$this->setHorizontalTotal (); <BR>} <BR>if ($this->isVerticalTotal) <BR>{ <BR>$this->setVerticalTotal (); <BR>} <BR>} <BR>/** <BR>* 设置纵向合计 <BR>*/ <BR>private function setVerticalTotal() <BR>{ <BR>$this->verticalColumn [] = $this->VERTICAL_TOTAL_FIELD; <BR>foreach ( $this->horizontalColumn as $i ) <BR>{ <BR>$rowsum = 0; <BR>foreach ( $this->verticalColumn as $j ) <BR>{ <BR>$rowsum += $this->pivotValue [$i] [$j]; <BR>} <BR>$this->pivotValue [$i] [$this->TOTAL_FIELD] = $rowsum; <BR>} <BR>} <BR>/** <BR>* 设置横向合计 <BR>*/ <BR>private function setHorizontalTotal() <BR>{ <BR>$this->horizontalColumn [] = $this->HORIZONTAL_TOTAL_FIELD; <BR>foreach ( $this->verticalColumn as $i ) <BR>{ <BR>$rowsum = 0; <BR>foreach ( $this->horizontalColumn as $j ) <BR>{ <BR>$rowsum += $this->pivotValue [$j] [$i]; <BR>} <BR>$this->pivotValue [$this->HORIZONTAL_TOTAL_FIELD] [$i] = $rowsum; <BR>} <BR>} <BR>/** <BR>* 渲染 <BR>*/ <BR>function Render() <BR>{ <BR>echo '<pre class="prettyprint linenums">'; <BR>print_r ( $this->pivotValue ); <BR>} <BR>/** <BR>* 渲染为table <BR>*/ <BR>function RenderToTable() <BR>{ <BR>$resault = "<table border='1' width='250'>\n"; <BR>$resault .= "<tr><td>$this->title</td>\n"; <BR>foreach ( $this->verticalColumn as $value ) <BR>{ <BR>$resault .= "<td>$value</td>\n"; <BR>} <BR>$resault .= "</tr>\n"; <BR>foreach ( $this->horizontalColumn as $i ) <BR>{ <BR>$resault .= "<tr><td>$i</td>\n"; <BR>foreach ( $this->pivotValue [$i] as $value ) <BR>{ <BR>$resault .= "<td>$value</td>\n"; <BR>} <BR>$resault .= "</tr>\n"; <BR>} <BR>$resault .= "</table>"; <BR>return $resault; <BR>} <BR>/** <BR>* 构造交叉表 <BR>* @param $data 数据源 <BR>* @param $topPivot 头栏目字段 <BR>* @param $leftPivot 左栏目字段 <BR>* @param $measure 计算量 <BR>*/ <BR>function __construct(array $data, $topPivot, $leftPivot, $measure) <BR>{ <BR>$this->data = $data; <BR>$this->leftPivot = $leftPivot; <BR>$this->topPivot = $topPivot; <BR>$this->measure = $measure; <BR>$this->horizontalColumn = array (); <BR>$this->verticalColumn = array (); <BR>$this->InitPivot (); <BR>$this->fillData (); <BR>} <BR>} <BR>
重点在于InitPivot方法及fillData方法。
InitPivot里面保证了所有的item都会有值(默认为0)
fillData方法使用选择填充添加的方法,将数据填充入我们装数据的$pivotValue里面。
然后喜欢怎么输出都可以了