0

我正在尝试创建一个条形图,显示过去 12 个月的每月点击量。

我有以下查询:

SELECT 
      (MONTH(date)) AS MONTH
    , (YEAR(date)) AS YEAR
    , SUM(hits) AS count 
FROM [statistics]
WHERE ID = '". $id ."' 
    AND (date > DATEADD(yy, - 1, GETDATE())) 
GROUP BY (MONTH(date)), (YEAR(date)) 
ORDER BY year, month

这提供了以下结果集:

Array
(
    [0] => Array
        (
            [Admin] => Array
                (
                    [month] => 7
                    [year] => 2012
                    [count] => 702
                )

        )

    [1] => Array
        (
            [Admin] => Array
                (
                    [month] => 8
                    [year] => 2012
                    [count] => 650
                )

        )

    [2] => Array
        (
            [Admin] => Array
                (
                    [month] => 9
                    [year] => 2012
                    [count] => 670
                )

        )

    [3] => Array
        (
            [Admin] => Array
                (
                    [month] => 10
                    [year] => 2012
                    [count] => 66
                )

        )

    [4] => Array
        (
            [Admin] => Array
                (
                    [month] => 11
                    [year] => 2012
                    [count] => 53
                )

        )

    [5] => Array
        (
            [Admin] => Array
                (
                    [month] => 12
                    [year] => 2012
                    [count] => 39
                )

        )

    [6] => Array
        (
            [Admin] => Array
                (
                    [month] => 1
                    [year] => 2013
                    [count] => 54
                )

        )

    [7] => Array
        (
            [Admin] => Array
                (
                    [month] => 2
                    [year] => 2013
                    [count] => 36
                )

        )

    [8] => Array
        (
            [Admin] => Array
                (
                    [month] => 3
                    [year] => 2013
                    [count] => 48
                )
        )
)

这仅显示它有结果的月份,但我需要所有月份,无论它们是否有结果(所以计数为 0)。我需要的是更像下面的东西,包括所有 12 个月:

   Array
    (
        [0] => Array
            (
                [Admin] => Array
                    (
                        [month] => 7
                        [year] => 2012
                        [count] => 702
                    )

            )

        [1] => Array
            (
                [Admin] => Array
                    (
                        [month] => 8
                        [year] => 2012
                        [count] => 650
                    )

            )

        [2] => Array
            (
                [Admin] => Array
                    (
                        [month] => 9
                        [year] => 2012
                        [count] => 670
                    )

            )

        [3] => Array
            (
                [Admin] => Array
                    (
                        [month] => 10
                        [year] => 2012
                        [count] => 66
                    )

            )

        [4] => Array
            (
                [Admin] => Array
                    (
                        [month] => 11
                        [year] => 2012
                        [count] => 53
                    )

            )

        [5] => Array
            (
                [Admin] => Array
                    (
                        [month] => 12
                        [year] => 2012
                        [count] => 39
                    )

            )

        [6] => Array
            (
                [Admin] => Array
                    (
                        [month] => 1
                        [year] => 2013
                        [count] => 54
                    )

            )

        [7] => Array
            (
                [Admin] => Array
                    (
                        [month] => 2
                        [year] => 2013
                        [count] => 36
                    )

            )

        [8] => Array
            (
                [Admin] => Array
                    (
                        [month] => 3
                        [year] => 2013
                        [count] => 48
                    )
            )
        [9] => Array
            (
                [Admin] => Array
                    (
                        [month] => 4
                        [year] => 2013
                        [count] => 0
                    )
            )
        [10] => Array
            (
                [Admin] => Array
                    (
                        [month] => 5
                        [year] => 2013
                        [count] => 0
                    )
            )
        [11] => Array
            (
                [Admin] => Array
                    (
                        [month] => 6
                        [year] => 2013
                        [count] => 0
                    )
            )
    )

谁能建议实现这一目标的最佳方法?可以在 SQL 中完成还是需要在代码中完成?如果是这样,怎么做?

提前谢谢了。

4

2 回答 2

0

这可以通过 PHP 使用循环来实现,该循环迭代所有可能的月份,如果该月有数据,则将其添加到输出数组,否则,插入该月计数为 0 的空白条目

编辑 示例解决方案;

$m = $resultset[0]['Admin']['month'];
$y = $resultset[0]['Admin']['year'];
for ($i = 0; $i < 12; $i++) {
    $adj = ((($i - 1) + $m) % 12) + 1;
    if (!(isset($resultset[$i]['Admin']['month']) && $resultset[$i]['Admin']['month'] == $adj)) {
        array_splice($resultset, $i, 0, array(array(
            'Admin' => array(
                'month' => $adj,
                'year'  => $y + ($i + $m > 12),
                'count' => 0
            )
        )));
    }
}

解决问题的重要部分是线路;

$adj = ((($i - 1) + $m) % 12) + 1;

$i从 0-11 之间的整数转换为 1-12 之间的月份,具体取决于起始月份 ( $m)

于 2013-06-06T10:58:53.890 回答
0

那么试试这个代码它可能会有所帮助

foreach ($array as $year => $values) {

        for ($i = 1; $i <= 12; $i++) { // Loop through months 1 .. 12

                if (!$values[$i]) {
                        echo "0";
                } else {
                        echo $values[$i];
                }
        }
}
于 2013-06-06T11:04:22.060 回答