显示从SQL数据库到Google图表的信息

时间:2020-05-11 12:50:05

标签: php html mysql sql google-visualization

我正在尝试将SQL数据库中的信息显示到Google图表中。 该项目是运行状况仪表板,我需要在其中显示步骤,kcal等。

我从数据库中获取了信息,但是在遍历它时遇到了麻烦(同一信息会显示多次)。我认为问题出在“ data.addrows”代码中。

php
include('template.php');
$query = /** @lang text */ <<
SELECT * FROM project_healthinfo WHERE id = {$_SESSION['userId']} ORDER BY date DESC
END;
$res = $mysqli->query($query);
$result = $res->fetch_object();
$content = <<<END
                  <!-- Google Chart script starts here -->
                  <!--Load the AJAX API-->



                    // Load the Visualization API and the corechart package.
                    google.charts.load('current', {'packages':['corechart']});

                    // Set a callback to run when the Google Visualization API is loaded.
                    google.charts.setOnLoadCallback(drawChart);

                    // Callback that creates and populates a data table,
                    // instantiates the pie chart, passes in the data and
                    // draws it.
                    function drawChart() {

                      // Create the data table.


                            google.charts.load('current', {'packages':['line']});
                            google.charts.setOnLoadCallback(drawChart);

                         google.charts.load('current', {'packages':['line']});
                      google.charts.setOnLoadCallback(drawChart);

                    function drawChart() {

                      var data = new google.visualization.DataTable();
                      data.addColumn('string', 'Date');
                      data.addColumn('number', 'Steps taken');


                      data.addRows([
                        ['$result->date',  $result->steps],
                        ['$result->date',  $result->steps],
                        ['$result->date',  $result->steps]
                      ]);

                      var options = {
                        chart: {
                          title: ''
                        },
                        width: 550,
                        height: 300
                      };

                      var chart = new google.charts.Line(document.getElementById('steps_chart'));

                      chart.draw(data, google.charts.Line.convertOptions(options));
                    }
                  </script>
                  <!-- Google Chart script ends here -->

1 个答案:

答案 0 :(得分:0)

您需要将$ content分为两部分。

$before_result_date = <<<EOS
...
EOS;

$after_result_data = <<<EOQ
...
EOS;

然后在这之间的部分应该有一个类似于此的循环。

while($result = $mysqli->fetch_object()){                      
   printf("['%s', %s],\n",$result->date, $result->steps);
}

所有这些都是这样的:

include('template.php');
$query = /** @lang text */ <<
SELECT * FROM project_healthinfo WHERE id = 
{$_SESSION['userId']} ORDER BY date DESC
END;

$res = $mysqli->query($query);

print $before_result_date;

while($result = $mysqli->fetch_object()){                      
   printf("['%s', %s],\n",$result->date, $result->steps);
}

print $after_result_date;

应该这样做。

相关问题