【发布时间】:2017-07-14 01:00:40
【问题描述】:
我正在尝试将我的数据库选择中的 json 编码推送到 chart.js 数据集中,但我不确定如何在不过度设计的情况下进行。
以下是从回顾表中简单选择的分数:
if($teamData == 0){
$allTeams = 'All';
} else{
$sql = "SELECT * FROM compScore WHERE memberId = 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$jsonScores = json_encode($row, JSON_PRETTY_PRINT);
}
}
}
header('Content-type: application/json');
echo $jsonScores;
这个输出是:
{ "id": "1", "score1": "2", "score2": "3", "score3": "5", "score4": "4", "score5": "3", "score6": "2", "score7": "1", "score8": "3", "memberId": "1" }
我有一个问题,它只获取 1 条记录,我还想排除字段 memberId
更何况我如何将结果推送到:
datasets : [
{
fillColor: "rgba(102,45,145,.1)",
strokeColor: "rgba(102,45,145,1)",
pointColor : "rgba(220,220,220,1)",
pointStrokeColor : "#fff",
data : [] // HERE IS WHERE THE DATA NEEDS TO GO
}
【问题讨论】:
-
首先从数组中删除不必要的元素。参考这个链接:stackoverflow.com/questions/4466159/…
-
之后将其编码为json格式并传递给js库部分。
-
@prakashtank 我不确定如何将它传递到 js 库中,这就是我有点卡住的地方