0

我在使用 flot 绘制数据库中的数据时遇到问题。在 php 中使用json_encode我试图获取绘图数据,但我没有成功。以下是我的代码,请帮忙

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>`enter code here`
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Flot Examples</title>
    <link href="./flot/layout.css" rel="stylesheet" type="text/css">
    <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="../excanvas.min.js"></script><![endif]-->
    <script language="javascript" type="text/javascript" src="./flot/jquery.js"></script>
    <script language="javascript" type="text/javascript" src="./flot/jquery.flot.js"></script>

 </head>
    <body>

    <h1>Database Plotting</h1>

    <div id="placeholder" style="width:600px;height:300px;"></div>

<?php

    $server = "localhost";
    $user="user";
    $password="password"; 
    $database = "database";

    $connection = mysql_connect($server,$user,$password);
    $db = mysql_select_db($database,$connection);

    $query = "SELECT x_value, y_value FROM table";
    $result = mysql_query($query);  

    while($row = mysql_fetch_assoc($result))
    {
        $dataset1[] = array($row['x_value'],$row['y_value']);
    }

?>


<script type="text/javascript">
$(function () {
    var dataset1 = <?php echo json_encode($dataset1,); ?>;

    $.plot($("#placeholder"),[dataset1] );
});
</script>

 </body>
</html>      

因此,来自的输出json-encode dataset1如下[["1380111342","0"],["1380111679","0"],["1380112099","20"],["1380112109","300"],["1380112114","40"],["1380112120","56"],["1380112127","36"],["1380112132","40"],["1380112138","78"]]

4

2 回答 2

0
var dataset1 = <?php echo json_encode($dataset1,); ?>;

去掉 "$dataset1" 后面的逗号和末尾的分号。

这应该可以正常工作:

var dataset1 = <?php echo json_encode($dataset1); ?>
于 2013-11-15T09:20:40.753 回答
0

尝试更改此行:

$.plot($("#placeholder"), [dataset1] );  

对此:

$.plot($("#placeholder"), [JSON.parse(dataset1)]);
于 2013-09-26T11:02:11.723 回答