将十进制字符串从php传递给jquery

时间:2016-07-09 18:20:38

标签: javascript php jquery html highcharts

我试图将一些格式化的字符串从PHP传递给jQuery。但我收到错误

  

VM2203:3未捕获的SyntaxError:浏览器中出现意外的数字。

这是我的代码

<?php
$OSlist = [];
$category = "";
$pieData = "";
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_PORT => "8081",
CURLOPT_URL => "http://localhost:8081/api/devices/hoursBy/OSVersion",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
    "cache-control: no-cache",
),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
echo "cURL Error #:" . $err;
} else {
$decodeStr = json_decode($response, true);
foreach ($decodeStr as $item) {
    $os = $item['_id']['os'];
    if ($os == $selectedOS) {
        if ($item['total_hours'] != 0) {
            $OSlist[$item['_id']['version']] = $item['total_hours'];
            // echo 'iOS'.$item['_id']['version'] . '&nbsp;:&nbsp;' . $item['total_hours'] ,"<br>";
        }
    }
}
ksort($OSlist);
foreach ($OSlist as $key => $val) {
    // echo "$key = $val" . "<br>";
    $category = $category . $key . ",";
    $pieData = $pieData . "[" . $key . "," . $val . "],";
}
$pieData="[".$pieData."]";
echo $pieData;
// print_r($OSlist);
 } ?>
 <script >
        $('document').ready(function () {
            var jsArray = <?php echo $pieData; ?>;
            alert(jsArray);

        });


    </script>

Piedata输出

[[2.3.4,97.9],[4.0.3,11.6],[4.0.4,2.4],[4.1.1,15.1],[4.1.2,398.4],[4.2.1,45.8],[4.2.2,27.3],[4.3,321],[4.4.2,694.9],[4.4.4,478.7],[5,646.3],[5.0.1,177.5],[5.0.2,1210.5],[5.1,126.2],[5.1.1,524.4],[6,201.5],]

我实际上需要将此数据传递给高图饼图到数据值。下图是针对移动版本与其使用小时数的对比。

function loadPieGraph() {
            // Radialize the colors
            var android = <?php echo $androidhrs ?>;
            var ios = <?php echo $ioshrs ?>;
            var obj = [{
                    type: 'pie',
                    name: 'Browser share',
                    data: [
                        ['4.1', '23.0'],
                        {
                            name: '5.0',
                            y: '40.2',
                            sliced: true,
                            selected: true
                        },
                    ]
                }];
            Highcharts.getOptions().colors = Highcharts.map(Highcharts.getOptions().colors, function (color) {
                return {
                    radialGradient: {
                        cx: 0.5,
                        cy: 0.3,
                        r: 0.7
                    },
                    stops: [
                        [0, color],
                        [1, Highcharts.Color(color).brighten(-0.3).get('rgb')] // darken
                    ]
                };
            });

            // Build the chart
            $('#container').highcharts({
                chart: {
                    plotBackgroundColor: null,
                    plotBorderWidth: null,
                    plotShadow: false,
                    type: 'pie'
                },
                title: {
                    text: 'Browser market shares. January, 2015 to May, 2015'
                },
                tooltip: {
                    pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
                },
                plotOptions: {
                    pie: {
                        allowPointSelect: true,
                        cursor: 'pointer',
                        dataLabels: {
                            enabled: true,
                            format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                            style: {
                                color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                            },
                            connectorColor: 'silver'
                        }
                    }
                },
                series: obj
            });


        }

1 个答案:

答案 0 :(得分:1)

整个部分:

foreach ($OSlist as $key => $val) {
    // echo "$key = $val" . "<br>";
    $category = $category . $key . ",";
    $pieData = $pieData . "[" . $key . "," . $val . "],";
}
$pieData="[".$pieData."]";
echo $pieData;
// print_r($OSlist);
 } ?>
 <script >
        $('document').ready(function () {
            var jsArray = <?php echo $pieData; ?>;
            alert(jsArray);

        });


    </script>

看起来真的很缺陷。

你应该宁愿:

$pieData = json_encode($OSlist);

并且在脚本标记中您只需执行此操作:

 jsArray = JSON.parse("<?php echo $pieData; ?>");

然后在JS中执行HighChart相关的数组格式。 或者你可以直接在PHP中进行格式化,然后对最终形式执行json编码并直接将其传递给js,再次需要JSON.parse它们。

在这些类型的案例中,始终以JSON的形式进行数据交易。

希望它有所帮助。

希望它有所帮助。