我想将我的变量放在 google 饼图中,但它总是从 select 中选择最后一个东西

I want to put my variables in the google Piechart but it keeps picking the last thing from the select

我从我的数据库中得到了这些答案,但我认为它在一个数组中,因为当我想把它放在我的图表中时,它会选择最后一个而不是 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 中一样改变值(我没有包含)。

这是数据库table 这是 phpmyadmin 中的计数查询:

你的所有台词:

['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。这就是您的浏览器收到的所有信息,因为您的网络浏览器不知道您的 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 请求相比,这非常丑陋,但它有效。