json_encode和highcharts

时间:2013-04-09 23:10:08

标签: php jquery mysql json highcharts

我正在尝试使用jQuery在PHP和MySQL中创建图形,特别是使用Highcharts库,我已经设法使用来自MySQL数据库的数据创建图表,但这些数据显示了一些数据,如果它们在数据库中但不在其他数据库中例如,一个产生10个结果5的查询是好的,但其他五个没有,我在网上做了一些研究,我理解这是因为我在MySQL中通过json_encode传递查询结果时出现问题,我希望你们中的一些人可以告诉我,因为我有这个错误,或者我是否误用了JSON,非常感谢你们。 码:  的index.php

    <html>
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
    <title>Incidencias</title>
    <script type="text/javascript" src="../js/jquery-1.7.1.min.js" ></script>
    <script type="text/javascript" src="../js/highcharts.js" ></script>
    <script type="text/javascript" src="../js/themes/grid.js"></script>
    <script type="text/javascript" src="../js/export.js"></script>
    <script type="text/javascript">
        var chart;
            $(document).ready(function() {
                var options = {
                    chart: {
                        renderTo: 'pieClientes',
                        defaultSeriesType: 'pie',
                        marginRight: 10,
                        marginBottom: 25
                    },
                    title: {
                        text: 'top 10. Clientes con m\u00e1s incidencias',
                        x: -10  
                    },
                    subtitle: {
                        text: '',
                        x: -20
                    },
                    tooltip: {
                        formatter: function() {
                            return '<b>'+ this.point.name +'</b>: <b>'+ this.y +' incidencias</b>';
                        }
                    },
                    legend: {
                        layout: 'vertical',
                        align: 'right',
                        verticalAlign: 'top',
                        x: -10,
                        y: 100,
                        borderWidth: 0
                    },

                series: [{
                    type: 'pie',
                    name: 'Browser share',
                    data: []
                }]
            }

            $.getJSON("datosCliente.php", function(json) {
                options.series[0].data = json;
                chart = new Highcharts.Chart(options);
            });
            });
    </script>
</head>
<body>
    <div style="font-size: 12p; font-family: Calibri, Candara, Segoe, Segoe UI, Optima, Arial, sans-serif; align="left" >
        <h1>Dashboard incidencias SCI</h1>
    <div>
    <div id="pieClientes" style="width: 770px; height: 450x; margin: 0"></div>              
</body>
</html>

datosCliente.php

<?php
    include"../clases/conexion.php";
    $result = mysql_query("SELECT 
                    clientes.cliente,
                    COUNT(incidencias.idIncidencia) AS nIncidencias
                    FROM incidencias
                    INNER JOIN clientes ON incidencias.idCliente = clientes.idCliente
                    GROUP BY incidencias.idCliente
                    ORDER BY nIncidencias DESC
                    LIMIT 0,10");
    $rows = array();
        while($r = mysql_fetch_array($result)) 
            {
                $row[0] = $r[0];
                $row[1] = $r[1];
                array_push($rows,$row);
            }

    //print json_encode($rows, JSON_PRETTY_PRINT);
    echo json_encode($rows, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT); 
    mysql_close($con);
?> 

错误是例如当我想在图表中显示10行数据库时,它会显示此表单的结果:

      1st. row->  show me "slice" 
      2nd. row-> the registerok,
      3th. row->  show me "slice"
      4. row-> the registerok,
      5. row-> the registerok,
      6.row-> show me "slice"
     7.row->the registerok,
     8.row-> show me "slice"
     9.row->the registerok,
    10.row-> show me "slice"

问题是在图形(饼图)中显示正确的数据但不显示其他数据,就好像某些记录或管道跳过了5个寄存器的限制,而不是我不知道我错在哪里。 :(

感谢您的帮助:)。

1 个答案:

答案 0 :(得分:0)

首先,我会尝试为数组元素赋值,而不是使用索引。为数组键命名。同样删除编码中的所有大写字母。我在ipad上无法编码,但如果您有疑问,请告诉我。