phpvariablesgoogle-pie-chart

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


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


Solution

  • All your lines with:

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

    Do have the same number, hence your result.

    In general: Just forget about PHP, and look first at the SOURCE that is send to the browser. This one was easily spotted if you did.

    And the problem lies in your PHP code which is not possible to debug without seeing where $count comes from.

    EDIT:

    To clarify:

    This part:

    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; ?>]
           ]);
    

    Always has the same $count. And that is all your browser receives because your webbrowser doesn't know about your PHP or database queries. (This is important to remember for future debugging sessions.)

    So a rude solution would be:

    (I assume your posted javascript comes from a PHP file in this example)

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

    Which is pretty ugly compared to an Ajax request, but it works.