簡體   English   中英

適用於Google Charts Api的PHP MySQL查詢數組json_encode

[英]PHP MySQL Query Array json_encode for Google Charts Api

我有以下代碼

$objDbResultByModel = $objDatabase->Query($modelQuery);
$return_arr = array();
echo "<h3>Total Model No<br></h3><strong>";
while ($mixRow = $objDbResultByModel->FetchArray()) {
    $row_array['Model'] = $mixRow['Manu']." - " .$mixRow['model'];
    $row_array['Quantity'] = $mixRow['total'];

        array_push($return_arr,$row_array);
$jsonTable = json_encode($return_arr);
echo $jsonTable;

我在瀏覽器中得到以下結果。

[ 
  {"Model No.":"HP - 3120-MT","Quantity":"1"},
  {"Model No.":"IBM - 6087-CN8","Quantity":"1"},
  {"Model No.":"Fujitsu - D2594","Quantity":"4"},
  {"Model No.":"Fujitsu - D2750","Quantity":"15"},
  {"Model No.":"HP - DC 7800","Quantity":"43"},
  {"Model No.":"HP - DC7700","Quantity":"1"},
  {"Model No.":"HP - DC7900","Quantity":"8"},
  {"Model No.":"Fujitsu - E5720","Quantity":"1"},
  {"Model No.":"RM - RM","Quantity":"5"},
  {"Model No.":"HP - XW4400","Quantity":"1"},
  {"Model No.":"HP - XW4600","Quantity":"2"}
]

但是,如果我將jsonTable與Google Charts API結合使用,它將無法正常工作。 我做錯了什么? 見下文。

<script type="text/javascript">

      // Load the Visualization API and the piechart package.
      google.load('visualization', '1.0', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);

      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {

        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Model');
        data.addColumn('number', 'Quantity');
        data.addRows([

<?php echo $jsonTable; ?>

        ]);

        // Set chart options
        var options = {'title':'Model Quantity in this Shipment',
                       'width':400,
                       'height':300};

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>

您能幫我解決這里的問題嗎?

DataTable無法使用您輸入的數據格式。 更改此:

$row_array['Model'] = $mixRow['Manu']." - " .$mixRow['model'];
$row_array['Quantity'] = $mixRow['total'];

對此:

$row_array = array($mixRow['Manu'] . " - " . $mixRow['model'], $mixRow['total']);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM