Arraynetworks PHP Array Cross-table implementation code

Source: Internet
Author: User
If the workload is too large to use the SQL statement, then try to write a cross-table class, so apart, let's look at the code

Copy the 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 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 Vertical 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 '

';
Print_r ($this->pivotvalue);
}
/**
* Render as Table
*/
function rendertotable ()
{
$resault = "















\ n "; $resault. = " \ n "; foreach ($this->verticalcolumn as $value) {$resault. = " \ n "; } $resault. = " \ n "; foreach ($this->horizontalcolumn as $i) {$resault. = " \ n "; foreach ($this->pivotvalue [$i] as $value) {$resault. = " \ n "; } $resault. = " \ n "; } $resault. = "
$this->title$value
$i$value
";
return $resault;
}
/**
* Construct cross-table
* @param $data Data source
* @param $topPivot header 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 item will have a value (default is 0)
The Filldata method fills the data into the $pivotvalue of the data we are loading using the method of selecting fill addition.
And then like how to output everything.

The above describes the Arraynetworks PHP array cross-table implementation code, including the arraynetworks aspect of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.