我想将变量放入Google Piechart中,但它会不断从选择中选择最后的内容

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我想将变量放入Google Piechart中,但它会不断从选择中选择最后的内容相关的知识,希望对你有一定的参考价值。

我从数据库中得到了这些答案,但我认为它位于数组中,因为当我要将其放入图表中时,它选择的是最后一个而不是1乘1。

我尝试将图表放入while循环中,但当时没有用。即使它确实与它的回声一起工作。

这是脚本:

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   <script type="text/javascript">
     google.charts.load('current', {'packages':['corechart']});
     google.charts.setOnLoadCallback(drawChart);

     function drawChart() {

       var data = google.visualization.arrayToDataTable([
         ['Task', 'Hours per Day'],
         ['A',      <?php echo $count; ?>],
         ['B',      <?php echo $count; ?>],
         ['C',      <?php echo $count; ?>],
         ['D',      <?php echo $count; ?>],
         ['E',      <?php echo $count; ?>],
         ['F',      <?php echo $count; ?>]
       ]);

       var options = {
         title: 'Aantal antwoorden:'
       };

       var chart = new google.visualization.PieChart(document.getElementById('piechart'));

       chart.draw(data, options);
     }
   </script>

这就是我获取变量的方式:

$aantal = $row['count(answer_id)'];
for ($meme = 1; $meme <= $aantal; $meme++) {
$countAnswerQuery = "SELECT answer_id, COUNT(*), question_id  FROM survey_answers WHERE question_id = '$meme' GROUP BY answer_id ORDER BY question_id ASC";
$countanswerresult = mysqli_query($conn, $countAnswerQuery);
if ($countanswerresult ->num_rows > 0) {
  while ($row = mysqli_fetch_array($countanswerresult)) {

    $question = $row['question_id'];
    $answer = $row['answer_id'];
    $count = $row['COUNT(*)'];

我希望能够将变量放入其中,并且它的值就像在echo中一样(我未包括在内)来改变值。

enter image description here

这是数据库表这是phpmyadmin中的count查询:enter image description here

答案

您所有带有的行:

['A',      <?php echo $count; ?>]
to
['F',      <?php echo $count; ?>]

具有相同的数字,因此您的结果。

通常:只需忘记PHP,首先查看发送到浏览器的SOURCE。如果您发现的话,这很容易被发现。

问题出在您的PHP代码中,如果不查看$ count的来源,则无法调试。

编辑:

澄清:

此部分:

var data = google.visualization.arrayToDataTable([
         ['Task', 'Hours per Day'],
         ['A',      <?php echo $count; ?>],
         ['B',      <?php echo $count; ?>],
         ['C',      <?php echo $count; ?>],
         ['D',      <?php echo $count; ?>],
         ['E',      <?php echo $count; ?>],
         ['F',      <?php echo $count; ?>]
       ]);

始终具有相同的$ count。这就是您的浏览器收到的所有信息,因为您的Web浏览器不了解您的PHP或数据库查询。 (这对于以后的调试会话要记住很重要。)

所以一个不礼貌的解决方案是:

((在此示例中,我假设您发布的javascript来自PHP文件)

    <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
       <script type="text/javascript">
         google.charts.load('current', {'packages':['corechart']});
         google.charts.setOnLoadCallback(drawChart);

         function drawChart() {

           var data = google.visualization.arrayToDataTable([
             ['Task', 'Hours per Day'],
<?php
$aantal = $row['count(answer_id)'];
for ($meme = 1; $meme <= $aantal; $meme++) {
$countAnswerQuery = "SELECT answer_id, COUNT(*), question_id  FROM survey_answers WHERE question_id = '$meme' GROUP BY answer_id ORDER BY question_id ASC";
$countanswerresult = mysqli_query($conn, $countAnswerQuery);
if ($countanswerresult ->num_rows > 0) {
  while ($row = mysqli_fetch_array($countanswerresult)) {

    $question = $row['question_id'];
    $answer = $row['answer_id'];
    $count = $row['COUNT(*)'];
    // I am guessing $answer contains A, B, C, not sure.
    echo "['" . $answer . "', {$count}],";
   }
}
?>

           ]);

           var options = {
             title: 'Aantal antwoorden:'
           };

           var chart = new google.visualization.PieChart(document.getElementById('piechart'));

           chart.draw(data, options);
         }
       </script>

与Ajax请求相比,这很丑陋,但是可以。

以上是关于我想将变量放入Google Piechart中,但它会不断从选择中选择最后的内容的主要内容,如果未能解决你的问题,请参考以下文章

如何将变量放入字符串中?

使用圆形边框和切片边距自定义 Google Visualization PieChart 外观

将纬度和经度放入距离矩阵,python中的google map API

如何为Piechart元素设置自定义颜色

将命令放入变量中[重复]

如何将厨师“执行资源”的输出放入变量中