【问题标题】:Rows into columns and / or crosstabbing array in php行成列和/或php中的交叉表数组
【发布时间】:2012-04-11 04:53:52
【问题描述】:

我有这个帐单表,我可以根据报告要求从中获取记录。

我得到的数组是这样的:

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

我试图让它按照我的要求工作,但 cud 不这样做。

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

【问题讨论】:

    标签: php mysql crosstab


    【解决方案1】:

    我认为使用 SQL 为您完成大部分数字运算会更好。

    比较简单的过程,那么:

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

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

    下面是一些代码示例,用于为给定的一组月-年组合生成查询(未经测试)。从这里把它放到一个表中很简单(你只需要在 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);
    

    【讨论】:

    • 没有帮助!如果您能举个例子,将不胜感激。请。我的表和数据结构是here
    • 现在应该比较清楚了,我希望。您可以直接从数据库中获取所需的所有行/列。只需对其进行格式化并添加总计行。
    • 太好了!谢谢你的一切。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多