2013-09-26 68 views
0

我有一個問題,使用flot繪製數據庫中的數據。 在php中使用json_encode我試圖獲取劇情數據,但我一直不成功。 下面是我的代碼,請協助flot和mysql jquery

<!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"]]

+0

請不要使用'mysql_ *'因爲它是貶值。使用'PDO'或'mysqli_ *' –

回答

0

嘗試改變這一行:

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

要的是:

$.plot($("#placeholder"), [JSON.parse(dataset1)]); 
+0

我確實使用了[JSON.parse(dataset1)],但仍然無法繪製。急需任何幫助。 – alexanda

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

丟失「$ dataset1」後面的逗號和末尾的分號。

這應該只是罰款:

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