0

I have these answers from my database but I think it's in a array because when I want to put it in my chart it selects the last one and not 1 by 1.

I tried putting the chart in the while loop but it didn't work then. Even though it does work with the echo of it.

This is the script:

<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>

And this is how I get the variables:

$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 hope to be able to put the variable in and that it changes value just like in the echo ( that I didn't include ).

enter image description here

This is the DB table and this is the count query in phpmyadmin: enter image description here

4

1 回答 1

2

你所有的台词:

['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 请求相比,这非常难看,但它确实有效。

于 2019-09-30T12:41:50.340 回答