Retrieve data from mysql by php to create flot gra

2019-02-01 12:44发布

Hi i am trying to retrieve data from mysql database to create flot graph can anyone walk me through this procedure or give me an idea of what to do thanks

标签: php flot
4条回答
ら.Afraid
2楼-- · 2019-02-01 13:03

You probably want something like this. I haven't used flot but I looked at the example here.

<?php
//create array of pairs of x and y values
$dataset1 = array();
while ($row = mysql_fetch_assoc()) { //or whatever
    $dataset1[] = array( $row['xvalue'], $row['yvalue'] );
}
?>

<script type="text/javascript">
    //put array into javascript variable
    var dataset1 = <?php echo json_encode($dataset1); ?>;

    //plot
    $(function () {
         $.plot($("#placeholder"), [ dataset1 ]);
    });
</script>
查看更多
做自己的国王
3楼-- · 2019-02-01 13:03

This depends largely on your environment and requirements. There's lots of free tools out there you can use. One example is Flot that lets you use jQuery to build graphs. There's link to documentation on the Google Code page.

查看更多
小情绪 Triste *
4楼-- · 2019-02-01 13:11

Adding upon the example from @Tom Haigh:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Flot Examples</title>
    <link href="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="../jquery.js"></script>
    <script language="javascript" type="text/javascript" src="../jquery.flot.js"></script>
 </head>
    <body>
    <h1>Flot Examples</h1>

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

<?php

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

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

query = "SELECT x_axis_values, y_axis_values FROM some_table";
    $result = mysql_query($query);        

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

?>


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

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

 </body>
</html>
查看更多
迷人小祖宗
5楼-- · 2019-02-01 13:20

as @Tom Haigh say work well, but you need to add another code to work well, I was using the example, but I discover in the source code it add to the result quotes " so to avoid this just add the: intval to the array, example:

<?php
$query = "SELECT valx, valy FROM chart";
    $result = mysql_query($query);        

    while($row = mysql_fetch_assoc($result))
    {
        $d2[] = array (intval($row['valx']),intval($row['valy']));
    }
?>
查看更多
登录 后发表回答