dygraph php动态生成数据库中的内容

时间:2015-10-13 07:40:58

标签: javascript php dygraphs

我正在使用Dygraph来显示图表。我让它像这样静态工作

<div id="graphdiv"></div>
<script type="text/javascript">
  g = new Dygraph(

    // containing div
    document.getElementById("graphdiv"),

    // CSV 
    "Date,High,Low\n" +
    "2015-05-07,75,40\n" +
    "2015-05-08,70,50\n" +
    "2015-05-09,80,60\n" +
"2015-05-10,60,40\n" +
 "2015-05-11,50,30\n" +
"2015-05-12,0,0\n"

  );

</script>

现在我想使用PHP动态生成数据库中的内容。我试过这样做。我创建了关联数组以从数据库中获取信息,然后将php绑定添加到

    <?php
//CREATE SQL STATEMENT
$sql_temperatures = "SELECT * FROM tbltemperatures";

//CONNECT TO MYSQL SERVER
require('inc-conndygraph.php');

//EXECUTE SQL STATEMENT
$rs_temperatures = mysqli_query($vconndygraph, $sql_temperatures);

//CREATE AN ASSOCIATIVE ARRAY
$rs_temperatures_rows = mysqli_fetch_assoc($rs_temperatures);

?>
<!doctype html>
<html>
<head>

<!-- LINK TO THE DYGRAPH LIBRARY -->
<script type="text/javascript" src="dygraph-combined-dev.js"></script>

</head>
<body>

<!-- CONTAINER HOLDING GRAPH -->
<div id="graphdiv"></div>

<script type="text/javascript">
  g = new Dygraph(

    // containing div
    document.getElementById("graphdiv"),

"Date,High,Low\n" +
    // CSV 
    <?php do { ?>

    "<?php echo json_encode($rs_temperatures_rows['tdate']); ?>,<?php echo json_encode($rs_temperatures_rows['thigh']); ?>,<?php echo json_encode($rs_temperatures_rows['tlow']); ?>\n"

    <?php } while ($rs_temperatures_rows = mysqli_fetch_assoc($rs_temperatures)); ?>

  );
</script>

理论上,这应该在函数中起作用,但是当我尝试在任何浏览器中查看时它都不会显示。我错过了一些愚蠢的东西吗?

我以为我可能连接不正确而且显示只是没有返回?我不擅长javascript,对此的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

java脚本中的do while循环应该如下所示

"Date,High,Low\n" +

<?php do { ?>

    "<?php echo $rs_temperatures_rows['tdate'] . ',' . $rs_temperatures_rows['thigh'] . ',' . $rs_temperatures_rows['tlow']; ?>'\n' +

    <?php } while ($rs_temperatures_rows = mysqli_fetch_assoc($rs_temperatures)); ?> '&nbsp;'