Table 在 google 个图表和 php 中没有专栏

Table has no column in google charts and php

我需要有关我的 google 图表的帮助...我目前有一个 getdata.php,其中包含来自我的数据库 table 的信息查询。我有一个页面可以启用一个给 select 一个特定的用户,一旦用户被 selected 我使用 &_GET 选项来获取一个 ID 并将它提供给 getData.php 中的查询,这样它就可以动态生成图表,结尾 product/chart 没有显示,因为我没有列消息。这是我的 getData.php 和 chart1.php 显示图表。

chart1.php

<script type="text/javascript">


// Load the Visualization API and the piechart package.


google.load('visualization', '1', {
'packages' : [ 'corechart' ]


 });

// Set a callback to run when the Google Visualization API is loaded.

 google.setOnLoadCallback(drawChart);

function drawChart() {
var jsonData = $.ajax({
  url : "getData.php?",
  type: "GET" , // or GET, depends on your choice data

  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.BarChart(document
    .getElementById('chart_div'));
chart.draw(data);
}

getData.php

if(isset($_SESSION['userid'])){
        $getid = $_GET['id'];

        $graphquery =" SELECT sa.userid
              ,q.question_group_id
              ,sum(pa.answer)/(select count(q.question)*3)*100 as 'Percentage'
        FROM surveyanswers sa
        INNER JOIN possibleanswers pa
        ON pa.possible_answer_id = sa.answer_voted
        INNER JOIN questions q
        ON q.question_id = sa.question_id
        WHERE userid='$getid'
        GROUP BY question_group_id";
        $result_set=mysql_query($graphquery, $connection);

        echo '{
          "cols": [
                {"id":"","label":"Groupname","pattern":"","type":"string"},
                {"id":"","label":"Percentage","pattern":"","type":"number"}
                ],
          "rows": [';
           while($row =mysql_fetch_assoc($result_set)){

               echo '{"c":[{"v":"'.$row['question_group_id'].'"},{"v":'.$row['Percentage'].'}]},';
           }

           echo ' ]
        }';

}

我认为您的 SELECT 字符串设置不正确。试试这个:

$graphquery ="SELECT sa.userid
          ,q.question_group_id
          ,sum(pa.answer)/(select count(q.question)*3)*100 as 'Percentage'
    FROM surveyanswers sa
    INNER JOIN possibleanswers pa
    ON pa.possible_answer_id = sa.answer_voted
    INNER JOIN questions q
    ON q.question_id = sa.question_id
    WHERE userid=" . $getid . " GROUP BY question_group_id";

您也可以在 运行 SQL 语句之前打印出 $graphquery 变量来检查它。

您似乎没有在 ajax 调用中通过 url 传递 id 变量。

var jsonData = $.ajax({
  url : "getData.php?id="+userid,
  type: "GET" , // or GET, depends on your choice data

  dataType : "json",
  async : false

}).responseText;

结果是 $getid 变量从未被填充,使您的 SQL 查询无效。显然,您必须确保以某种方式填充 userid 变量。

所以问题在于我如何传递数据..我添加了一个名为数据的额外字段,并在 chart1.php 上通过它传递了我的 php get id ...在 getdata.php 我调用了 $_GET[id],它现在动态生成图表。花了很多时间,但我猜它是一个学习曲线。

chart1.php

 function drawChart() {
var jsonData = $.ajax({
  url : "getData.php",
  type: "GET" , // or GET, depends on your choice data
  data:"id=<?php echo "$_GET[id]";?>", 
  dataType : "json",
  async : false

}).responseText;

getdata.php

        $graphquery =" SELECT q.question_group_id
   , qg.group_name
   ,sum(pa.answer)/(select
   count(q.question)*3)*100 as 'Percentage' 
  FROM surveyanswers sa


INNER JOIN possibleanswers pa
on  pa.possible_answer_id = sa.answer_voted



INNER JOIN questions q
on q.question_id = sa.question_id
and userid=$_GET[id]

INNER JOIN questionsgroup qg
on qg.question_group_id= q.question_group_id
 group by question_group_id;";



       $result_set=mysql_query($graphquery, $connection);