php - 谷歌图和 PHP

标签 php mysql database graph echo

我想显示一个从数据库表中获取信息的线图。我发现了 Google Graphs,它似乎最适合我的需要。我可以让图表显示我在代码中放置的数据,但是当我尝试使用 echo 从数据库添加数据时,它不起作用。请在下面找到我的代码。我不确定我在这里做错了什么。

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">

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

      // Set a callback to run when the Google Visualization API is loaded.
      google.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.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Date');
        data.addColumn('number', 'Number of Products');
        data.addRows([
<!--CHANGE FOR PHP TO PULL INFORMATION FRO MYSQL DBA-->

<?php
// Create connection
$con=mysqli_connect("host","username","password","database");

// Check connection
if (mysqli_connect_errno($con))
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $result = mysqli_query($con,"SELECT * FROM table");
$start = 0;
while($row = mysqli_fetch_array($result)){

    echo "['".$row["dateadded"]."', ".$row["products"]."],";

mysqli_close($con);
?>
        ]);

        // Set chart options
        var options = {'title':'Product Count History',
                       'width':600  ,
                       'height':550};

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.LineChart(document.getElementById('chart_product_history'));
        chart.draw(data, options);
      }
    </script>


                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                <td width="50%">
                <div id="chart_product_history"></div>
                </td>
                <td width="50%">

                </td>
                </tr>
                <tr>
                <td width="50%">

                </td>
                <td width="50%">

                </td>
                </tr>
                </table>

谢谢瑞安

最佳答案

我用一些固定的随机数据尝试了您的代码,它确实生成了一个图表(尽管数组中有一个额外的逗号有时可能会导致问题)。

您似乎在查询结果周围的循环中缺少右大括号。

尝试以下操作

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">

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

      // Set a callback to run when the Google Visualization API is loaded.
      google.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.
        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Date');
        data.addColumn('number', 'Number of Products');
        data.addRows([
<!--CHANGE FOR PHP TO PULL INFORMATION FRO MYSQL DBA-->

<?php
// Create connection
$con=mysqli_connect("host","username","password","database");

// Check connection
if (mysqli_connect_errno($con))
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM table");
$start = 0;
$OutArray = array();
while($row = mysqli_fetch_array($result))
{
    $OutArray[] = "['".$row["dateadded"]."', ".$row["products"]."],";
}

echo explode(',', $OutArray);

mysqli_close($con);
?>
        ]);

        // Set chart options
        var options = {'title':'Product Count History',
                       'width':600  ,
                       'height':550};

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.LineChart(document.getElementById('chart_product_history'));
        chart.draw(data, options);
      }
    </script>


                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                <td width="50%">
                <div id="chart_product_history"></div>
                </td>
                <td width="50%">

                </td>
                </tr>
                <tr>
                <td width="50%">

                </td>
                <td width="50%">

                </td>
                </tr>
                </table>

关于php - 谷歌图和 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18251987/

相关文章:

php - 使用 PHP 打印 MySQL 数据库中的元素作为相应图像的链接

php - 浏览器中不显示特殊字符

mysql - 一次插入多个

SQL 脚本 - 检查 IP 是否已列入黑名单

php - 为 doctrine2 查询添加所有相关项的计数

php - 使用php从xml中提取信息

php - mySQL:将值插入到具有外键关系的2个表中

mysql - 防止重复输入数据库

php - 根据数据库值从选项中进行选择

php - 使用 PHP 在 MySql 数据库中插入数据时遇到问题