2015-10-13 59 views
2

我使用Dygraph来显示图形。我把它静态这样工作dygraph php动态生成数据库中的内容

<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和任何帮助,将不胜感激。

回答

0

该做的,而Java脚本内循环应该是这样的

"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;'