使用PDO构建堆积柱形图

时间:2018-02-10 19:14:38

标签: php

我第一次尝试学习如何构建堆积柱形图,但我不知道我在做什么,我会告诉你我使用的查询

$chart = $DB_con->prepare("
        SELECT   DATE_FORMAT(submitdate, '%M') AS SubmitMonth, 
                 SUM(unified) AS UnifiedAmount,
                 EXTRACT(YEAR_MONTH FROM submitdate) As SubmitYearMonth
        FROM     sales 
        WHERE    submitdate IS NOT NULL 
             AND EXTRACT(YEAR_MONTH FROM submitdate) >= EXTRACT(YEAR_MONTH FROM CURDATE())-100 
             AND unified IS NOT NULL 
             AND unified > 0
        GROUP BY DATE_FORMAT(submitdate, '%M')
        ORDER BY SubmitYearMonth");
        $chart->execute();
        $results = $chart->fetchAll(PDO::FETCH_ASSOC);

foreach ( $results AS $row){
$dataPoints1 = array();
$dataPoints1["label"] = $row["SubmitMonth"];
$dataPoints1["y"] = $row["UnifiedAmount"];
}

但它似乎没有像我预期的那样工作,

预期结果

$dataPoints1 = array(
array("label"=> "January", "y"=> 3289),
array("label"=> "February", "y"=> 5312),
array("label"=> "March", "y"=> 11020)

);

1 个答案:

答案 0 :(得分:2)

尝试:

$dataPoints1 = array();
foreach ($results as $row) {
    $dataPoints1[] = [
        "label" => $row["SubmitMonth"],
        "y" => $row["UnifiedAmount"]
    ];
}