Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve data from mysql by php to create flot graph

Tags:

php

flot

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

like image 286
Sarah Avatar asked Aug 21 '09 14:08

Sarah


1 Answers

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>
like image 92
John M Avatar answered Sep 20 '22 03:09

John M