If you use SQL statements to do too much work, so try to write a crosstab class, good apart, let's look at the code
Copy Code code as follows:
/**
* Basic Cross Table
* @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 ';
/**
* Initialize the Cross table
*/
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;
}
}
}
/**
* Fill Data
*/
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 ();
}
}
/**
* Set Portrait totals
*/
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;
}
}
/**
* Set Horizontal totals
*/
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;
}
}
/**
* Rendering
*/
function Render ()
{
Echo ' <pre> ';
Print_r ($this->pivotvalue);
}
/**
* Render as Table
*/
function rendertotable ()
{
$resault = "<table border= ' 1 ' width= ' >\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;
}
/**
* Construct a cross table
* @param $data Data source
* @param $topPivot header column field
* @param $leftPivot left column field
* @param $measure Calculation amount
*/
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 emphasis is on Initpivot method and Filldata method.
Initpivot guarantees that all the item will have a value (default is 0)
The Filldata method populates the $pivotvalue of our data with the method of selecting fill additions.
And then like how to output it all.