如何将php数组块传递给jquery函数?

时间:2015-05-30 06:23:21

标签: javascript php jquery

我试图将php数组传递给我的jQuery函数,我已经完成并正常工作,我正面临格式化问题。以下是详细信息。

默认功能

drawHeroArea : function () {

    !verboseBuild || console.log('proton.graphsStats.drawHeroArea()');
    if($('#hero-area').length)

    proton.graphsStats.graph.Area = Morris.Area({

        element: 'hero-area',

        data: [

          {period: '2010Apr Q1', iphone: 2666, ipad: null, itouch: 2647},

          {period: '2010 Q2', iphone: 2778, ipad: 2294, itouch: 2441},

          {period: '2010 Q3', iphone: 4912, ipad: 1969, itouch: 2501},

          {period: '2010 Q4', iphone: 3767, ipad: 3597, itouch: 5689},

          {period: '2011 Q1', iphone: 6810, ipad: 1914, itouch: 2293},

          {period: '2011 Q2', iphone: 5670, ipad: 4293, itouch: 1881},

          {period: '2011 Q3', iphone: 4820, ipad: 3795, itouch: 1588},

          {period: '2011 Q4', iphone: 15073, ipad: 5967, itouch: 5175},

          {period: '2012 Q1', iphone: 10687, ipad: 4460, itouch: 2028},

          {period: '2012 Q2', iphone: 8432, ipad: 5713, itouch: 1791}

        ],

        xkey: 'period',

        ykeys: ['iphone', 'ipad', 'itouch'],

        labels: ['iPhone', 'iPad', 'iPod Touch'],

        pointSize: 2,

        hideHover: 'auto'

    });

},

我想在此函数中传递数据

 $LoopD['mydata'] = array();
 foreach($tData as $mon=>$val){ 

        $LoopD[]  =  array(

        'period' => $mon, 
        'iphone' => $val['amber'], 
        'ipad' =>  $val['red'],
        'itouch' => $val['green'],
            );

}
$jsonL  = json_encode($LoopD);  

使用Javascript:

var jsonL       = $jsonL;

$(document).ready(function(e) {
            drawHeroArea(jsonL);

                });

我将数据传递给函数

drawHeroArea : function (jsonL) {

        !verboseBuild || console.log('proton.graphsStats.drawHeroArea()');
        if($('#hero-area').length)

        proton.graphsStats.graph.Area = Morris.Area({

            element: 'hero-area',

            data: [
            jsonL
],

            xkey: 'period',

            ykeys: ['iphone', 'ipad', 'itouch'],

            labels: ['iPhone', 'iPad', 'iPod Touch'],

            pointSize: 2,

            hideHover: 'auto'

        });

    }

到目前为止我已经尝试但无法找到结果我想在我的jquery函数中使用数据文件格式。

{period: '2010 Q2', iphone: 2778, ipad: 2294, itouch: 2441}

1 个答案:

答案 0 :(得分:1)

if ($result != false) { $total_records = mysqli_num_rows($result); if ($total_records == 0) { echo 'No results founds.'; } } 已经是一个数组,所以在jsonL而不是这个:

drawHeroArea

使用它:

data: [
    jsonL
],

还要确保替换它:

data: jsonL

有了这个:

$LoopD['mydata'] = array();