PHP Array Cross table implements code _php skills

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.