ChartJS - 将'foreach'查询数据转换为JSON

时间:2017-05-26 11:15:34

标签: javascript php json foreach chart.js

所以我正在开展一个相当大的学校项目,我们的目标是显示我们学校所有学生的存在与否,我们必须使用chartJS显示这些数据,现在我们做了一个选择所有课程和foreach循环遍历类和查询以选择存在并将其转换为JSON,我已经在这个函数上工作了好几天但我似乎无法让它工作...... < / p>

这是我的QueryManager,用于激活查询:

public function getAanwezigheid($klassen){
    $result = $this->dbconn->query("SELECT klas.code klas, ROUND(
(
    SELECT Count(aanwezigheid) 
    FROM aanwezigheid 
    JOIN college ON aanwezigheid.Ccode = college.code  
    JOIN klas ON college.Kcode = klas.code
    WHERE klas.code = '".$klassen."' AND vak.code = 'WFHBOICT.M032.16' AND college.college = '8'
    AND aanwezigheid = '1'
) 
/
(
    SELECT Count(aanwezigheid) 
    FROM aanwezigheid 
    JOIN college ON aanwezigheid.Ccode = college.code  
    JOIN klas ON college.Kcode = klas.code
    WHERE klas.code = '".$klassen."' AND vak.code = 'WFHBOICT.M032.16' AND college.college = '8'
) 
* 100)
    as percentage
FROM aanwezigheid 
JOIN college ON aanwezigheid.Ccode = college.code 
JOIN klas ON college.Kcode = klas.code 
JOIN vak ON college.Vcode = vak.code 
WHERE klas.code = '".$klassen."' AND vak.code = 'WFHBOICT.M032.16' AND college.college = '8'
GROUP BY klas.code");
    $data = array();
    foreach ($result as $row) {
    $data[] = $row;
    }
    print json_encode($data);
}

以下是获取查询的代码(unserialize获取所有可用的类A-F类):

if($_GET['action']=='getAanwezigheid'){
    header('Content-Type: application/json');
    $klas = unserialize($_SESSION['klas']);

    /*This loop runs the query for every class which has been found (A-F)*/
    foreach($klas as $klas){
    $klassen = $klas->getCode();
    $result = $q->getAanwezigheid($klassen); 
    }
}

以下是我得到的结果:

[][][{"klas":"WFHBOICT.V1C","percentage":"38"}][][][{"klas":"WFHBOICT.V1F","percentage":"67"}]

我希望结果如下:

[{"klas":"WFHBOICT.V1C","percentage":"38"},{"klas":"WFHBOICT.V1F","percentage":"67"}]

现在我知道为什么我得到所有括号,这是因为我也在每个类中循环数据[],但这就是我所坚持的。我只需要打印1个JSON结果

任何帮助都会非常感激。

0 个答案:

没有答案