表没有列。如何添加列标题

时间:2018-04-16 09:16:55

标签: php json charts google-visualization sqlsrv

您好我有一个代码可以成功地将数据从我的SQLSVR提取到JSON记录

<?php

$db = new PDO("sqlsrv:Server=DK-MatthewClay\SQLExpress2012;Database=FFP_WebServices", "LocalAdmin", "");

$row=$db->prepare("FFP_WebServices.dbo.WEBSERV_DAILY_PERIOD_FIGURES_SELECT");

$row->execute();//execute the query  
$json_data=array();//create the array  

foreach($row as $rec)//foreach loop  
{  
  $json_array['Dates']=$rec['Dates'];
  $json_array['OrdersTD']=$rec['OrdersTD'];
  $json_array['OrdersAD']=$rec['OrdersAD'];

  //here pushing the values in to an array  
  array_push($json_data,$json_array);  
}  

//built in PHP function to encode the data in to JSON format  
echo json_encode($json_data, JSON_NUMERIC_CHECK);  
?> 

这会像我这样构建我的JSON

  

[{&#34;日期&#34;:&#34; 2018/03/26#34;&#34; OrdersTD&#34;:86043&#34; OrdersAD&#34;:85900.55} {&#34;日期&#34;:&#34; 2018/03/27#34;&#34; OrdersTD&#34;:86043&#34; OrdersAD&#34;:46288.81}]

但是没有列标题。所以,当我尝试使用此代码时,我无法创建一个谷歌图表,任何帮助都会很棒

这是我尝试创建图表

<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">

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

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

function drawChart() {
  var jsonData = $.ajax({
      url: "includes/buildJson.php",
      dataType: "json",
      async: false
      }).responseText;

  // Create our data table out of JSON data loaded from server.
  var data = new google.visualization.DataTable(jsonData);

  // Instantiate and draw our chart, passing in some options.
  var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  chart.draw(data, {width: 400, height: 240});
}

</script>
</head>

<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

直接从json创建DataTable时,json必须采用某种格式,
在这里找到 - &gt; Format of the Constructor's JavaScript Literal data Parameter

请参阅以下php以生成所需的json ...

<?php

$db = new PDO("sqlsrv:Server=DK-MatthewClay\SQLExpress2012;Database=FFP_WebServices", "LocalAdmin", "");

$row=$db->prepare("FFP_WebServices.dbo.WEBSERV_DAILY_PERIOD_FIGURES_SELECT");

$row->execute();//execute the query

//create the array
$json_data['cols'] = array(
  array('label' => 'Dates', 'type' => 'string'),
  array('label' => 'OrdersTD', 'type' => 'number'),
  array('label' => 'OrdersAD', 'type' => 'number')
);

$rows = array();
foreach($row as $rec)//foreach loop
{
  $temp = array();
  $temp[] = array('v' => (string) $rec['Dates']);
  $temp[] = array('v' => (float) $rec['OrdersTD']);
  $temp[] = array('v' => (float) $rec['OrdersAD']);
  $rows[] = array('c' => $temp);
}
$json_data['rows'] = $rows;

//built in PHP function to encode the data in to JSON format
echo json_encode($json_data, JSON_NUMERIC_CHECK);
?>

另外,ajax上的async: false已被弃用,请改用done回调...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  $.ajax({
    url: 'includes/buildJson.php',
    dataType: 'json'
  }).done(function (jsonData) {
    var data = new google.visualization.DataTable(jsonData);
    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, {width: 400, height: 240});
  });
});