dseslyh6662605 2017-10-13 07:35
浏览 40
已采纳

通过ajax填充多维数组

Update

I get an error syntax error, unexpected '{', expecting ']' on the line below:

<?php
$data['datasets'] = [{
                        label: 'Quotation',
                        backgroundColor: '#96ccf1',
                        borderWidth: 1,
                        data: [
                            100,500,5000,800,500
                        ]
                    },{
                        label: 'Purchase Order',
                        backgroundColor: '#96ccf1',
                        borderWidth: 1,
                        data: [
                            100,500,5000,800,500
                        ]
                    },{
                        label: 'Invoice',
                        backgroundColor: '#96ccf1',
                        borderWidth: 1,
                        data: [
                            100,500,5000,800,500
                        ]
                    }];
?>

I have the original js code which I want to change into dynamic data retrieval. I'm able to change the labels part but unable to change the datasets part.

Given the original data looks like (refer below), how to amend my PHP code so that when passed through ajax it can successfully display the correct data? Am I missing some quotes here and there??

Thank you.

Original JS code (static)

$.ajax({
    url: "action.php",
    type: "POST",
    data: {'action':'RetrieveData'},
    dataType: "json",
    success: function(data) {
        console.log(data);

        if(data.status=='success'){
            var barChartData = {
                labels: ["01-Oct-2017","07-Oct-2017","14-Oct-2017","21-Oct-2017","28-Oct-2017"],
                datasets: [{
                            label: 'Quotation',
                            backgroundColor: '#96ccf1',
                            borderWidth: 1,
                            data: [
                                100,500,5000,800,500
                            ]
                        }, {
                            label: 'Purchase Order',
                            backgroundColor: '#FAD84E',
                            borderWidth: 1,
                            data: [
                                100,500,2500,800,500
                            ]
                        }, {
                            label: 'Invoice',
                            backgroundColor: '#9FFA4E',
                            borderWidth: 1,
                            data: [
                                100,500,2000,800,0
                            ]
                        }]
            };

            var ctx = document.getElementById("canvas").getContext("2d");
            window.myBar = new Chart(ctx, {
                type: 'bar',
                data: barChartData,
                options: {
                    responsive: true,
                    title: {
                        display: true,
                        text: data.title_text
                    },
                    tooltips: {
                        mode: 'index',
                        intersect: false
                    }
                }
            });
        }
    },
    error: function(data){
        console.log('Error occurred');
    }
});

Amended JS code (dynamic)

$.ajax({
    url: "action.php",
    type: "POST",
    data: {'action':'RetrieveData'},
    dataType: "json",
    success: function(data) {
        console.log(data);

        if(data.status=='success'){
            var barChartData = {
                labels: data.labels, // <-- This is correct
                datasets: data.datasets // <-- I need help on this part
            };

            var ctx = document.getElementById("canvas").getContext("2d");
            window.myBar = new Chart(ctx, {
                type: 'bar',
                data: barChartData,
                options: {
                    responsive: true,
                    title: {
                        display: true,
                        text: data.title_text
                    },
                    tooltips: {
                        mode: 'index',
                        intersect: false
                    }
                }
            });
        }
    },
    error: function(data){
        console.log('Error occurred');
    }
});

PHP code

$data['labels'] = ["01-Oct-2017","07-Oct-2017","14-Oct-2017","21-Oct-2017","28-Oct-2017"];
$data['datasets'] = [{
                        label: 'Quotation',
                        backgroundColor: '#96ccf1',
                        borderWidth: 1,
                        data: [
                            100,500,5000,800,500
                        ]
                    },{
                        label: 'Purchase Order',
                        backgroundColor: '#96ccf1',
                        borderWidth: 1,
                        data: [
                            100,500,5000,800,500
                        ]
                    },{
                        label: 'Invoice',
                        backgroundColor: '#96ccf1',
                        borderWidth: 1,
                        data: [
                            100,500,5000,800,500
                        ]
                    }];

echo json_encode($data); 
  • 写回答

1条回答 默认 最新

  • dongmiyu8979 2017-10-13 08:02
    关注

    If you want to create an object like {label: 'some name'} you can use a class or an associative array ['label' => 'some name']. PHP isn't be able to create one of the previous mentioned elements out of an {}.

    $data['labels'] = ["01-Oct-2017","07-Oct-2017","14-Oct-2017","21-Oct-2017","28-Oct-2017"];
    $data['datasets'] = [[
                            'label' => 'Quotation',
                            'backgroundColor' => '#96ccf1',
                            'borderWidth' => 1,
                            'data' => [
                                100,500,5000,800,500
                            ]
                        ],[
                            'label' => 'Purchase Order',
                            'backgroundColor' => '#96ccf1',
                            'borderWidth' => 1,
                            'data' => [
                                100,500,5000,800,500
                            ]
                        ],[
                            'label' => 'Invoice',
                            'backgroundColor' => '#96ccf1',
                            'borderWidth' => 1,
                            'data' => [
                                100,500,5000,800,500
                            ]
                        ]];
    
    echo json_encode($data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: