在php中进入列和/或交叉排列数组

时间:2012-04-11 04:53:53

标签: php mysql crosstab

我有这个帐单表,其中我根据报告要求获得记录。

我得到的数组是这样的:

Array(
[0] => stdClass Object
    (
        [bid] => 3
        [uid] => 2
        [total_inc] => 100
        [total_exp] => 55
        [mon] => 1
        [year] => 2012
        [mstdatereg] => 2012-03-14
    )

[1] => stdClass Object
    (
        [bid] => 2
        [uid] => 3
        [total_inc] => 85
        [total_exp] => 45
        [mon] => 1
        [year] => 2012
        [mstdatereg] => 2012-03-14
    )

[2] => stdClass Object
    (
        [bid] => 1
        [uid] => 8
        [total_inc] => 130
        [total_exp] => 75
        [mon] => 1
        [year] => 2012
        [mstdatereg] => 2012-03-14
    )

[3] => stdClass Object
    (
        [bid] => 5
        [uid] => 25
        [total_inc] => 130
        [total_exp] => 65
        [mon] => 2
        [year] => 2012
        [mstdatereg] => 2012-03-14
    )

[4] => stdClass Object
    (
        [bid] => 4
        [uid] => 27
        [total_inc] => 75
        [total_exp] => 50
        [mon] => 2
        [year] => 2012
        [mstdatereg] => 2012-03-14
    )

[5] => stdClass Object
    (
        [bid] => 10
        [uid] => 3
        [total_inc] => 180
        [total_exp] => 100
        [mon] => 3
        [year] => 2012
        [mstdatereg] => 2012-04-05
    )

[6] => stdClass Object
    (
        [bid] => 6
        [uid] => 12
        [total_inc] => 60
        [total_exp] => 35
        [mon] => 3
        [year] => 2012
        [mstdatereg] => 2012-03-14
    )

[7] => stdClass Object
    (
        [bid] => 7
        [uid] => 22
        [total_inc] => 160
        [total_exp] => 90
        [mon] => 3
        [year] => 2012
        [mstdatereg] => 2012-03-14
    )

[8] => stdClass Object
    (
        [bid] => 9
        [uid] => 3
        [total_inc] => 115
        [total_exp] => 70
        [mon] => 4
        [year] => 2012
        [mstdatereg] => 2012-03-16
    )
)

我通过循环完成的是结果,如下所示:

        January, 2012
        ==========
        Income  Expense
    2   100     55
    3   85      45
    8   130     75
    ---------------------------------
Total   315     175

        February, 2012
        ===========
        Income  Expense
    25  130     65
    27  75      50
    ---------------------------------
Total   205     115

        March, 2012
        Income  Expense
    3   180     100
    12  60      35
    22  160     90
    ---------------------------------
Total   400     225

        April, 2012
        Income  Expense
    3   115     70
    ---------------------------------
Total   115     70

Net Total Income: 1035
Net Total Expense: 585code here

然而我想要的是

Sr.No   Member  1, 2012     2, 2012     3, 2012     4, 2012     Total
                Inc|Exp     Inc|Exp     Inc|Exp     Inc|Exp     Inc|Exp 
=======================================================================
1       2       100|55                                          100|55
2       3       85|45                   180|100     115|70      380|215
3       8       130|75                                          130|75  
4       25                  130|65                              130|65
5       27                  75|50                               75|50
6       12                              60|35                   60|35
7       22                              160|90                  160|90  
=======================================================================
Total           315|175     205|115     400|225     115|70      1035|585

在发布之前,我搜索了这个问题,这就是我找到的:Building a "crosstab" or "pivot" table from an array in php

我试图按照我的要求使其工作但不反过来。

现在已经尝试了很多天。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我认为使用SQL为你做大部分数字运算会更好。

相对简单的过程,然后:

  1. 确定要在表格中表示的月份(列)。
  2. 使用GROUP BY将数据分成Sr.No(或您想要使用的任何内容)
  3. 使用WHERE子句删除不在您选择的日期范围内的数据(即,如果报告仅适用于2月和3月,则不包括1月)
  4. 总计列是给定行的收入或费用的总和(请记住,行使用您之前选择的标准进行分组)
  5. 个别月份列是IF(日期在此列的月份,金额,0)
  6. 的总和
  7. 总计行在您的PHP
  8. 中计算

    您可以通过编程方式生成月份列代码。

    下面是一些代码示例,用于为给定的一组月 - 月组合生成查询(未经测试)。从这里将它放入表中是很简单的(你只需要在PHP中计算总计行)。

    $qstr = 'SELECT bid, uid, ';
    $reportMonths = array(1 => 2012, 2 => 2012, 3 => 2012, 4 => 2012);
    
    foreach ($reportMonths as $mon => $year) {
        $qstr .= "SUM(IF(mon=$mon AND year=$year),total_inc,0)) as $mon-inc, SUM(IF(mon=$mon AND year=$year,total_exp,0)) as $mon-exp, ";
    }
    
    $qstr .= 'SUM(total_inc) as total-inc, SUM(total_exp) as total-exp from bill_mst WHERE ';
    
    foreach ($reportMonths as $mon => $year) {
        $qstr .= '(mon=$mon AND year=$year) OR ';
    }
    // chop off last or
    $qstr = substr($qstr, 0, -3);
    $qstr .= 'GROUP BY bid, uid ASC';
    
    $res = mysql_query($qstr);