2013-04-09 73 views
2

我想在PHP和MySQL中使用jQuery製作圖表,特別是使用Highcharts庫,我設法使用來自MySQL數據庫的數據製作圖表,但這些數據顯示一些數據(如果它們在數據庫中)但不是其他人,例如,產生10個結果的查詢5是好的,但其他5個沒有,我在網絡上做了一些研究,我知道這是由於當我將查詢的結果傳遞給MySQL by json_encode,我希望你們中的一些人可以告訴我,因爲我有這個錯誤,或者如果我濫用JSON,非常感謝。 代碼: 的index.phpjson_encode和highcharts

<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" 

的問題是,在曲線圖(餅圖)顯示器正確的數據而不是其他人,這是因爲如果一些記錄或tubiese跳過5的限制registrros,而不是我不知道我錯在哪裏。 :(

感謝幫助:)

+0

什麼是錯誤? – Phil 2013-04-09 23:11:18

+0

您的數據/ JSON的外觀如何?你是否按x升序排列數據? – 2013-04-11 09:48:45

回答

0

首先我會盡量給數組元素的值,而不是使用索引。給你的數組鍵一個名字。同時刪除編碼中的所有大寫字母。我在iPad上,無法編碼,但如果您有任何問題,請告訴我。