tags:

views:

886

answers:

2

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

A: 

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.

Steven Surowiec
+9  A: 

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>
Tom Haigh
Hi i did as you said but it isn't working there is an exception $dataset1 = array(); while ($row = mysql_fetch_assoc($sql)) { $dataset1[] = array( $row['msgCount'], $row['Group_ID'] ); } echo json_encode($dataset1);jquery function plotGraph() { alert("In body"); $.ajax({ url:"getData.php", type:"post", datatype:"json", success:function(data) {alert(data); $.plot($("#placeholder"), [ data ]); }, error:function() {alert("There was a problem");} }) }is there something missing
Sarah
Hey Tom This example was really helpful but now i am facing a problem of handling the null because it is not plotted
Sarah
@Sarah: I suppose you could convert them to zero - either in the query e,g IFNULL( colName, 0 ) or in your PHP loop by casting each value to an integer.
Tom Haigh
Thanks for this, was having trouble getting the json in from php to flot and this worked perfectly
xiaohouzi79